我正在製作一個文本編輯器應用程序,將其文檔的每個文檔存儲爲NSFileWrapper目錄,文檔文本和文檔標題作爲目錄中的單獨文件。我期望部分loadFromContents: (id) contents
是一個NSFileWrapper,但事實並非如此。我的代碼如下(它屬於UIDocument的子類):UIDocument loadFromContents應該返回一個NSFileWrapper;返回NSConcreteData
// 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;
}
當我試圖調用這個方法,我得到這個錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData fileWrappers]: unrecognized selector sent to instance 0x9367150'
下面是我contentsForType:
功能,如果有幫助:
- (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;
}
謝謝!