【问题标题】:UIDocument loadFromContents fails with EXC_BAD_ACCESS [duplicate]UIDocument loadFromContents 失败并出现 EXC_BAD_ACCESS [重复]
【发布时间】:2012-12-03 21:31:55
【问题描述】:

我有一个使用 iCloud 文档存储的非常简单的 IOS 应用程序。一切正常,然后在某个时候,我开始在我的文档加载方法中遇到至少一个 iCloud 文档的 EXC_BAD_ACCESS 错误,尽管大多数文件加载正常。

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {

    file = (NSFileWrapper*) contents;

    NSFileWrapper *infoFile = [[file fileWrappers] objectForKey:InfoFile];
    NSData *infoData = [infoFile regularFileContents];

    if(nil != infoData) {

        NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
        NSError *propertyListError;

        // EXC_BAD_ACCESS occurs here
        NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:infoData options:NSPropertyListImmutable format:&format error:&propertyListError];

        if(nil == propertyListError) {

            _name = [dictionary objectForKey:@"name"];
            _date = [dictionary objectForKey:@"date"];
            _index = [dictionary objectForKey:@"index"];
            _paperSize = [GritzPaperSizeEnum enumWithType:[dictionary objectForKey:@"paperSize"]];

            TFLog(@"loading doc %@", _name);

            _pages = [[NSMutableArray alloc] init];

            for (NSString *key in file.fileWrappers) {

                NSFileWrapper *subDir = [[file fileWrappers] objectForKey:key];

                if(subDir.isDirectory) {
                    GritzPage *page = [[GritzPage alloc] initFromFile:subDir];
                    [_pages addObject:page];
                }
            }

            _currentPage = [_pages objectAtIndex:0];

            return YES;
        }
    }

    return NO;
}

我希望我可以“捕获”并处理不良数据并忽略损坏的文件;但我似乎无法弄清楚如何。 EXC_BAD_ACCESS 错误会导致应用崩溃。

我应该做些什么不同的事情来提前确定数据或文件将失败并跳过它(或删除它)。

【问题讨论】:

  • 我也有类似的问题...你能告诉我们你是如何解决的吗?这对我很有帮助。

标签: ios icloud uidocument


【解决方案1】:

使用 isKindOfClass 验证它是一个 NSFileWrapper,否则将其视为一个很奇怪(还请查看给定的 typeName :))


使用 @try { .. } @catch 构造来捕获任何异常在这种情况下都不会起作用,尽管你会导致 BAD_ACCESS 这是一个 UNIX 信号

【讨论】:

  • 我尝试检查“内容”和“信息文件”的文件类型,并且都作为 NSFileWrappers 传递。无论如何,我都希望如此,因为尝试将“infoData”转换为 NSDictionary 时会发生故障。我怀疑“infoData”文件包含无效的 XML,因此无法反序列化。我希望我可以用异常/错误处理代码来识别它,但是没有办法处理 BAD_ACCESS 错误。
【解决方案2】:

NSPropertyListFormat 变量format 应该声明为点。我认为你应该调用 propertyListWithData: 方法,格式为指针,而不是 format 的地址。

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2014-03-16
    • 2013-11-03
    相关资源
    最近更新 更多