首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDocument loadFromContents应该返回NSFileWrapper;而返回NSConcreteData

UIDocument loadFromContents应该返回NSFileWrapper;而返回NSConcreteData
EN

Stack Overflow用户
提问于 2013-07-02 16:16:04
回答 1查看 557关注 0票数 1

我正在制作一个文本编辑器应用程序,它将其每个文档存储为一个NSFileWrapper目录,其中文档文本和文档标题作为目录中的单独文件。我希望loadFromContents: (id) contentsloadFromContents: (id) contents部分是一个NSFileWrapper,但事实并非如此。我的代码如下(它属于UIDocument的一个子类):

代码语言:javascript
复制
// 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:函数,如果它有帮助的话:

代码语言:javascript
复制
- (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;

}

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-03 02:28:22

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17430620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档