Search code examples
iphoneobjective-ccocoansfilewrapper

Replacing NSFileWrappers


I have a NSFileWrapper directory in which I would like to update a certain FileWrapper. I was wondering what the best way is to do so?

So far I've used this code:

[self.fileWrapper addRegularFileWithContents:photoData 
                           preferredFilename:@"photo.data"];

However, whenever the FileWraper already exists, I get duplicates in my FileWrapper which look so:

"1__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bb0260>";
"2__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6b89b80>";
"3__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6ba1700>";
"4__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bc8480>";
"photo.data" = "<NSFileWrapper: 0x6bcfc50>";

How can I prevent this and simply replace the FileWrapper - in this case photo.data? I did not find any method to replace FileWrappers in the NSFileWrapper Class Reference.


Solution

  • I think this may be the solution:

    NSFileWrapper *oldFileWrapper = [self.fileWrapper.fileWrappers objectForKey:fileName];
    if (oldFileWrapper) [self.fileWrapper removeFileWrapper:oldFileWrapper];
    
    [self.fileWrapper addRegularFileWithContents:[self encodeObject:object] 
                               preferredFilename:fileName];