【问题标题】:UIDocument loadFromContents should return a NSFileWrapper; returns NSConcreteData insteadUIDocument loadFromContents 应该返回一个 NSFileWrapper;而是返回 NSConcreteData
【发布时间】:2013-07-02 16:16:04
【问题描述】:

我正在制作一个文本编辑器应用程序,将其每个文档存储为 NSFileWrapper 目录,文档文本和文档标题作为目录中的单独文件。我希望loadFromContents: (id) contentscontents 部分是一个NSFileWrapper,但事实并非如此。我的代码如下(它属于 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;
}

当我试图调用这个方法时,我得到这个错误:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSConcreteData fileWrappers]: 无法识别的选择器发送到实例 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;

}

谢谢!

【问题讨论】:

    标签: ios nsdata uidocument nsfilewrapper


    【解决方案1】:

    我解决了这个问题。我的 Documents 文件夹中有一个 .DS_Store 文件,它弄乱了结果。一旦我添加了一个 if 语句来排除它,一切正常。

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2017-11-10
      • 1970-01-01
      相关资源
      最近更新 更多