merah merah - 19 days ago 9
iOS Question

UIDocument loadFromContents should return a NSFileWrapper; returns NSConcreteData instead

I am making a text editor app that stores each of its document as a NSFileWrapper directory, with document text and document title as separate files in the directory. I expect the

contents
part of
loadFromContents: (id) contents
to be a NSFileWrapper, however this is not the case. My code is below (it belongs to a subclass of UIDocument):

// loads document data to application data model

- (BOOL) loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
// from the contents, extract it so that we have the properties initialized
self.fileWrapper = (NSFileWrapper *) contents;
NSLog(@"%@", [contents class]); //** returns NSConcreteData

// get the fileWrapper's children
NSDictionary *contentsOfFileWrapper = [contents fileWrappers];

// assign things to the document!
// can also be done lazily through getters

self.text = [contentsOfFileWrapper objectForKey:TEXT_KEY];
self.title = [contentsOfFileWrapper objectForKey:TITLE_KEY];
if ([self.delegate respondsToSelector:@selector(noteDocumentContentsUpdated:)]){
[self.delegate noteDocumentContentsUpdated:self];
}
return YES;
}


When I tried to call this method, I get this error:


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData fileWrappers]:
unrecognized selector sent to instance 0x9367150'


Below is my
contentsForType:
function if it helps:

- (id) contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
if (!self.fileWrapper) {
self.fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
}

NSDictionary *childrenFileWrappers = [self.fileWrapper fileWrappers];

// now if we have a text but it is not represented, in the file wrapper, put it in. Same with the images.
if ([childrenFileWrappers objectForKey:TEXT_KEY] == nil && self.text != nil) {
NSData *textData = [self.text dataUsingEncoding:kCFStringEncodingUTF16];
NSFileWrapper *textFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:textData];
[textFileWrapper setPreferredFilename:TEXT_KEY];
[self.fileWrapper addFileWrapper:textFileWrapper];
}

if ([childrenFileWrappers objectForKey:TITLE_KEY] == nil && self.title != nil) {
NSData *titleData = [self.title dataUsingEncoding:kCFStringEncodingUTF16];
NSFileWrapper *titleFileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:titleData];
[titleFileWrapper setPreferredFilename:TITLE_KEY];
[self.fileWrapper addFileWrapper:titleFileWrapper];
}

return self.fileWrapper;

}


Thanks!

Answer

I fixed the problem. There was a .DS_Store file in my Documents folder and it messed up the results. Once I added an if statement to rule it out everything worke fine.

Comments