0

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?

Community
  • 1
  • 1
Justin Copeland
  • 1,891
  • 5
  • 23
  • 27
  • 3
    possible duplicate of [How to store custom objects in NSUserDefaults](http://stackoverflow.com/q/2315948/); also http://stackoverflow.com/q/7459585/, http://stackoverflow.com/q/537044/, and http://stackoverflow.com/q/471830/ – jscs Apr 12 '12 at 19:48

2 Answers2

3

NSUserDefaults can 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 an NSData before putting it into NSUserDefaults (or into a collection which you're putting into NSUserDefaults or writing to a property list).

Putting custom classes in NSUserDefaults doesn't happen often in most apps, so you might also consider whether your Animals really belong there. Remember, NSUserDefaults is 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.

rickster
  • 124,678
  • 26
  • 272
  • 326
-3

you may check out Archiveor Serialization

Elf Sundae
  • 1,575
  • 16
  • 23