【发布时间】:2011-11-25 08:19:48
【问题描述】:
我想这很明显,但我有一个关于加载数据的问题。如果有一个名为 library.dat 的文件,它存储有关应用程序中对象的所有类型的信息。它设置得很好(就 initWithCoder 和 encodeWithCoder 方法等而言),但我只是想知道如果 library.dat 被损坏会发生什么。我自己把它弄坏了一点,然后应用程序就会崩溃。有什么方法可以防止崩溃吗?我可以在加载文件之前对其进行测试吗?这是可能非常致命的一点:
-(void)loadLibraryDat {
NSLog(@"loadLibraryDat...");
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"library.dat"];
// if the app crashes here, there is no way for the user to get the app running- except by deleting and re-installing it...
self.libraryDat = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
我查看了 *NSInvalidUnarchiveOperationException 但不知道应该如何在我的代码中实现它。我会很感激任何例子。提前致谢!
【问题讨论】:
标签: iphone objective-c ios cocoa-touch nskeyedarchiver