I'm working on creating an app that uses a class I created myself, Entry, to store specific data in an array. However, I keep running into this error when I try to access one of the values stored in an Entry object. Every time I prompt the user to create one of these objects, it is added to "rawArray," which is in turn stored into [NSUserDefaults standardUserDefaults]. At these lines of code, the program crashes:
Entry *currEntry = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawArray"] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);
I'm only trying to pull the array element that corresponds to the current UITableView section, and then NSLog the "date" variable stored in it. Here is the getDate method from the Entry class:
-(NSString *)getDate{
return date;
}
The date object itself is just another NSString in the Entry class, which is set in the class's init. If anyone can help me sort through this problem, I'd be incredibly thankful.
Here is the full crash log:
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
After I switched over to using NSKeyedArchiver/Unarchiver, I still ran into the same problem. Here are my new retrieve and save methods:
-(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;
}
Here are the new lines of code that crash the program:
Entry *currEntry = [[self loadFromUserDefaults] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);
And here is the updated crash log:
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