NSCoding和NSKeyedArchiving - “文档无法加载”

时间:2012-06-28 13:57:13

标签: cocoa nskeyedarchiver nsdocument nscoding

我已经为我的类实现了NSCoding协议,我在NSDocument子类中使用以下代码来保存和加载:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
[[record window] endEditingFor:nil];
return [NSKeyedArchiver archivedDataWithRootObject:self];

}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError{
@try {
    NSLog(@"Loading...");
    self = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
@catch (NSException *exception) {
    if (outError) {
        NSDictionary *d = [NSDictionary dictionaryWithObject:@"The data is corrupted" forKey:NSLocalizedFailureReasonErrorKey];
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:d];
    }
}
NSLog(@"whiteMoves count: %ld",[whiteMoves count]);

}

这一切似乎都很好。我可以保存一个文件,当我打开它并逐步执行代码时,它们似乎都可以解压缩并解码。但是,在“加载事件”完成后,我的应用程序总是弹出一个错误窗口,显示“文档”xxx.xxx“无法加载”。我无法找到触发此错误的位置或任何文档。

是否有人知道它来自哪里或知道它可能被记录在哪里?

由于

1 个答案:

答案 0 :(得分:1)

您似乎没有从YES返回NOreadFromData

- (BOOL)readFromData:(NSData *)data
              ofType:(NSString *)typeName
               error:(NSError **)outError
{
    BOOL retval = YES;
    @try
    {
        NSLog(@"Loading...");
        self = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        NSLog(@"whiteMoves count: %ld",[whiteMoves count]);
    }
    @catch (NSException *exception)
    {
        if (outError != nil)
        {
            NSDictionary *d = [NSDictionary dictionaryWithObject:@"The data is corrupted" forKey:NSLocalizedFailureReasonErrorKey];
            *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:d];
        }
        retval = NO;
    }

    return retval;
}

这与国际象棋有关;我可以问你在写什么吗?