gpt4 book ai didi

ios - UIDocument loadFromContents 应该返回一个 NSFileWrapper;改为返回 NSConcreteData

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:18 25 4
gpt4 key购买 nike

我正在制作一个文本编辑器应用程序,它将其每个文档存储为 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;
}

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

*** 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;

}

谢谢!

最佳答案

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

关于ios - UIDocument loadFromContents 应该返回一个 NSFileWrapper;改为返回 NSConcreteData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17430620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com