我正在创建一个应用程序,该应用程序使用我自己创建的类Entry将特定数据存储在数组中。但是,当我尝试访问存储在 Entry 对象中的值之一时,我一直遇到此错误。每次我提示用户创建这些对象之一时,它都会被添加到“rawArray”中,而后者又存储到 [NSUserDefaults standardUserDefaults] 中。在这些代码行中,程序崩溃了:
Entry *currEntry = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawArray"] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);
我只是试图拉出与当前 UITableView 部分相对应的数组元素,然后 NSLog 将“日期”变量存储在其中。这是 Entry 类的 getDate 方法:
-(NSString *)getDate{
return date;
}
日期对象本身只是 Entry 类中的另一个 NSString,它是在类的 init 中设置的。如果有人能帮我解决这个问题,我将非常感激。
这是完整的崩溃日志:
2012-04-24 16:31:32.545 Know[7278:f803] -[__NSCFString getDate]: unrecognized selector sent to instance 0x68598f0
2012-04-24 16:31:32.546 Know[7278:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString getDate]: unrecognized selector sent to instance 0x68598f0'
*** First throw call stack:
(0x13cb052 0x155cd0a 0x13ccced 0x1331f00 0x1331ce2 0xa930 0xb85c9 0xb8480 0x1fc849 0x1ff722 0xaf7c7 0xaf2c1 0xb228c 0xb6783 0x61301 0x13cce72 0x1d7592d 0x1d7f827 0x1d05fa7 0x1d07ea6 0x1d9330c 0x33530 0x139f9ce 0x1336670 0x13024f6 0x1301db4 0x1301ccb 0x12b4879 0x12b493e 0x22a9b 0x1d98 0x1cf5)
terminate called throwing an exception
在我切换到使用 NSKeyedArchiver/Unarchiver 之后,我仍然遇到了同样的问题。这是我的新检索和保存方法:
-(void)saveToUserDefaults:(NSMutableArray *)array{
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:array];
[[NSUserDefaults standardUserDefaults] setObject:myEncodedObject forKey:@"rawArray"];
}
-(NSMutableArray *)loadFromUserDefaults{
NSData *myDecodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"];
NSMutableArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
return decodedArray;
}
以下是使程序崩溃的新代码行:
Entry *currEntry = [[self loadFromUserDefaults] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);
这是更新的崩溃日志:
2012-04-24 17:43:36.853 Know[10314:f803] -[__NSCFString getDate]: unrecognized selector sent to instance 0x6acc260
2012-04-24 17:43:36.904 Know[10314:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString getDate]: unrecognized selector sent to instance 0x6acc260'
*** First throw call stack:
(0x13ce052 0x155fd0a 0x13cfced 0x1334f00 0x1334ce2 0xb710 0xbb5c9 0xbb480 0x1ff849 0x202722 0xb27c7 0xb22c1 0xb528c 0xb9783 0x64301 0x13cfe72 0x1d7892d 0x1d82827 0x1d08fa7 0x1d0aea6 0x1d9630c 0x36530 0x13a29ce 0x1339670 0x13054f6 0x1304db4 0x1304ccb 0x12b7879 0x12b793e 0x25a9b 0x2a18 0x2975)
terminate called throwing an exception