Search code examples
objective-ccocoansuserdefaultsproperty-list

Crash while Attempting to set Non Property List Objects in NSUserDefault


I tried to set a NSString object as below:

[[NSUserDefaults standardUserDefaults] setObject:[[self formModel] attributeAsString:@"someKey"] forKey:@"someValueKey"];

It doesn't crash every time. At some point it crashes saying "Attempt to set a non-property-list object as an NSUserDefaults value"

Can anybody help me to set this value properly and get it whenever requires?

Thanks in Advance.


Solution

  • If you want to store object other then NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary, you should archive it to create an instance of NSData.

    You can use propertyList:isValidForFormat: method of NSPropertyListSerialization class to validate property list