First of all: Merry Christmas everyone! It has been some time since I was able to store data into NSUserDefaults.
In this case I want to store a NSMutableArray to the NSUserDefaults. It is clear to me that you can only store immutable objects to this "file".
I had tried the following code for loading the NSMutableArray:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.mutableArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"TestKey"]];
I am using the following code for storing the data:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *tempArr = [NSArray arrayWithArray:self.mutableArray];
[defaults setObject:tempArr forKey:@"TestKey"];
[defaults synchronize];
Unfortunately, the simulator exits with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSUserDefaults setObject:forKey:]: attempt to insert non-property list object (
"<MyObject: 0x10dfb40a0>"
) for key TestKey'
The mutable array contains NSObjects.
Does anyone have a clue where I am screwing up?
self.mutableArray? - zaphNSUserDefaultscan not containNSObjects, see the documentation: A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. You will have to convert yourNSObjectto one of these. - zaph