1

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.

Colas
  • 3,473
  • 4
  • 29
  • 68
  • why not to directly update the file? – Anoop Vaidya Mar 10 '14 at 12:25
  • 1
    I can't write directly in the document's file. Otherwise, the application will complain that the file has been modified by another application. – Colas Mar 10 '14 at 14:01
  • The compiler that ships with Xcode 7 throws a warning with this code: `Null passed to a callee that requires a non-null argument` for `-[NSFileWrapper initDirectoryWithFileWrappers:]`. – Hyperbole Oct 22 '15 at 12:57

0 Answers0