Replacing NSFileWrappers
Asked Answered
C

2

8

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.

Carrie answered 20/6, 2012 at 15:35 Comment(0)
C
7

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];
Carrie answered 20/6, 2012 at 15:57 Comment(0)
A
0

I just stumbled upon this issue and for me it was because I had a folder named "Resources" and one named "resources". This created an iCloud issue because on Mac OS X the file system isn't case sensitive, but it is on iOS.

It looks like the underlying problem in your case might also be upper/lowercase related. Of course you're fixing this by replacing the file wrapper, which effectively deletes and recreates the file. This might be an acceptable solution for small files, but can be very inefficient for larger or many files in a directory (because the file would be synced even if it didn't change).

Arp answered 8/9, 2014 at 21:34 Comment(2)
OS X filesystems can be case-sensitive or case-insensitive...it depends on how the user formats the drive.Vacillating
That's a valid point. I am assuming the default format (as in when you buy a Mac) "Mac OS Extended (Journaled)".Arp

© 2022 - 2024 — McMap. All rights reserved.