I'd like to save an NSMutableDictionary
object in NSUserDefaults
. The key type in NSMutableDictionary
is NSString
, the value type is NSArray
, which contains a list of object which implements NSCoding
. Per document, NSString
and NSArray
both are conform to NSCoding
.
I am getting this error:
[NSUserDefaults setObject: forKey:]: Attempt to insert non-property value…. of class NSCFDictionary.
Best Answer
I found out one alternative, before save, I encode the root object (
NSArray
object) usingNSKeyedArchiver
, which ends withNSData
. Then use UserDefaults save theNSData
.When I need the data, I read out the
NSData
, and useNSKeyedUnarchiver
to convertNSData
back to the object.It is a little cumbersome, because i need to convert to/from
NSData
everytime, but it just works.Here is one example per request:
Save:
Load:
The element in the array implements
Then in the implementation of
CommentItem
, provides two methods:Anyone has better solution?
Thanks everyone.