0

我的 Iphone 项目在 viewDidLoad 事件中有问题,应用程序崩溃

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

我正在尝试存储来自文本归档的信息有人可以帮助我解决问题吗

- (void)viewDidLoad{
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i = 0; i < 2; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = array[i];
        }
        NSData *data = [[NSMutableData alloc]
                        initWithContentsOfFile:filePath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
                                         initForReadingWithData:data];
        BIDThreeLines *threelines = [unarchiver decodeObjectForKey:kRootKey];
        [unarchiver finishDecoding];

        for (int i = 0; i < 2; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = threelines.lines[i];
        }
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(applicationWillResignActive:)
     name:UIApplicationWillResignActiveNotification
     object:app];
}
Error 
2013-03-25 23:29:45.592 MobilePaymentsApp[1182:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0
2013-03-25 23:29:45.593 MobilePaymentsApp[1182:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0'
*** First throw call stack:
(0x1c96012 0x10d3e7e 0x1d214bd 0x1c85bbc 0x1c8594e 0x1c0ae18 0xb030e8 0x339c 0xf91c7 0xf9232 0x483d5 0x4876f 0x48905 0x51917 0x2cc5 0x15157 0x15747 0x1694b 0x27cb5 0x28beb 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1617a 0x17ffc 0x29fd 0x2925)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

https://github.com/a-elnajjar/MobilePaymentsApp

4

2 回答 2

0

NSKeyedArchiver 返回您存储在其中的对象。例如。如果您存储了一个数组,那么它将返回一个数组。所以在取消归档对象时要小心。

在以下示例中,我从 NSKeyedUnarchiver 读取了一个数组。

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
于 2013-03-26T04:17:43.200 回答
-1

查看崩溃日志的堆栈跟踪以查看此调用的确切位置。

如果您发送 -row 的变量实际上没有被键入为 NSArray,则很可能您没有遵循该变量的内存管理规则。这些相同的症状通常是由它引起的。响应 -row 的东西可能曾经存在过,因为你没有 -retain 它而被释放,然后在那个地方分配了一个 NSArray 。

运行“构建和分析”并重新查看内存管理指南,直到您在睡梦中了解它们。

来源:[NSCFArray 行]:无法识别的选择器发送到实例 0x3953a20

于 2013-03-26T04:10:01.683 回答