How to update an NSFileWrapper?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 04:16:32

问题


I have an NSFileWrapper with some content. I want to save it (writeToURL...:), add new content to it and save it again.

I would like to avoid writing again and again the same files. So, if I understood clearly, it means the option NSFileWrapperWritingAtomic is not for me. Especially as I save the file wrapper in the method

- (BOOL)writeAdditionalContent:(id)content
                         toURL:(NSURL *)absoluteURL
           originalContentsURL:(NSURL *)absoluteOriginalContentsURL
                         error:(NSError *__autoreleasing *)error

(of an OSX analogue of UIManagedDocument)

and as Apple says "don't use this option in an override of -[NSDocument writeToURL:ofType:error:]".

So, I don't use this option, but updating the files from the file wrapper (I mean : saving again) fails (see below). Any idea/clue ?


Here is what I do :

1

I save it a first time with

[self.fileWrapper writeToURL:[absoluteURL URLByAppendingPathComponent:@"attached_files/"]
                     options:0
         originalContentsURL:[absoluteOriginalContentsURL URLByAppendingPathComponent:@"attached_files/"]
                       error:error] ;

It works well.

My file wrapper is

    /*
     Initializing the file wrapper
     */
    _fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] ;
    [_fileWrapper setFilename:@"attached_files"] ;
    [_fileWrapper setPreferredFilename:@"attached_files"] ;

2

I save a second time with the same command. I get the error

 Error Domain=NSCocoaErrorDomain Code=516 "The file already exists". 

The given name is actually "attached_files".

3

If I use the option NSFileWrapperWritingWithNameUpdating, I still get the same error.

4

If I use the option NSFileWrapperWritingAtomic, I don't get errors.

来源:https://stackoverflow.com/questions/22299615/how-to-update-an-nsfilewrapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!