iOS - get NSDictionary from NSUserDefaults :Attempted to dereference an invalid ObjC Object or send it an unrecognized selector
ios, iphone, nsarray, nsuserdefaults
Solution
When you save your `Tremp` object to the defaults, you are actually saving it as a dictionary.
But when you read it out, your code assumes you have an array of `Tremp` objects.
You want something like:
for (NSDictionary *trempDict in myTrempsArray) {
Tremp *tremp = ... // add code here to create a Tremp from the dictionary
if([tremp.trempId isEqualToString:@"1234"]) {
[myTrempsArray removeObject:tremp];
break;
}
}
BTW - this code will crash. You can't modify an array that you are fast-enumerating through. Change the loop to be a standard `for` loop but go through the loop in reverse.
Also, when you save the data, replace the call to `setValue:forKey:` to `setObject:forKey:`.
Problem
I try to retrieve `NSMutableArray` from `NSUserDefaults` that have been saved. I store the `NSMutableArray`: ``` NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSMutableArray* mySavedTremps = [[defaults objectForKey:UD_MY_TREMPS] mutableCopy]; if (!mySavedTremps) mySavedTremps =[[NSMutableArray alloc] init]; NSMutableDictionary* trempDict = NSMutableDictionary* trempDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"please", @"help", @"me" @"!", nil] [trempDict setValue:trempId forKey:@"trempId"]; [mySavedTremps insertObject:trempDict atIndex:0]; [defaults setObject:mySavedTremps forKey:UD_MY_TREMPS]; [defaults synchronize]; ``` And try to retrieve the `NSMutableArray`: ``` NSMutableArray* myTrempsArray = [NSMutableArray arrayWithArray:[defaults objectForKey:UD_MY_TREMPS]]; for (Tremp* tremp in myTrempsArray) { if([tremp.trempId isEqualToString:@"1234"]) { [myTrempsArray removeObject:tremp]; break; } } ``` But, when I access tremp (param in the for loop) like: ``` tremp.trempId ``` I get this error: ``` error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector. ``` The process has been returned to the state before expression evaluation.