Possible Duplicate:
Storing custom objects in an NSMutableArray in NSUserDefaults
How to store custom objects in NSUserDefaults
I tried to store an object instantiated from a class I wrote called “Animal” in an NSMutableArray to be stored in NSUserDefaults. However, I get this error.
[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '(
"<Animal: 0xcea7060>"
)' of class '__NSArrayM'. Note that dictionaries and arrays in property lists must also contain only property values.
What does this mean? Perhaps I cannot store objects in NSMutableArrays to be stored in NSUserDefaults?
NSUserDefaultscan store arrays and dictionaries, mutable or not, but only if they contain property-list-legal classes (NSNumber,NSString,NSData, and a few others). For other classes, you should serialize the object(s) into anNSDatabefore putting it intoNSUserDefaults(or into a collection which you’re putting intoNSUserDefaultsor writing to a property list).Putting custom classes in
NSUserDefaultsdoesn’t happen often in most apps, so you might also consider whether yourAnimals really belong there. Remember,NSUserDefaultsis for settings, state, and other small things — the data your app manages are better kept elsewhere. Use archiving or property lists to write files in your app’s sandbox, or Core Data (or some other database) for such things.