I’m trying to load an array from the NSUserDefaults in an iPhone application. I set things up with this code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.myarray_1 = nil;
but then my application crashes at the following line:
self.myarray_1 = [[NSMutableArray alloc]
initWithArray: [defaults objectForKey:@"highscores"]];
with the error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSCFString count]: unrecognized selector sent to instance 0x796c710'
How can I solve this?
The error message says that an instance of
NSStringreceived acountmessage, but obviously it does not respond to it (i.e. does not implement such a message). Most likely the message was intended for anNSArrayinstance.Did you accidentally save an
NSStringinstead of anNSArrayin your defaults for the key@"highscores"? Maybe in one of your previous builds? To clear all user defaults, you could just delete the app from the simulator or your device and reinstall it.