如何更新NSFileWrapper?(How to update an NSFileWrapper?

2019-10-19 12:43发布

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.

文章来源: How to update an NSFileWrapper?