Question

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.

Was it helpful?

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

OTHER TIPS

Are you sure that actually returns an NSString (or subclass of NSString), and not, say, an NSAttributedString?

Are you sure it's that line? Try adding a breakpoint on 'abort', and then waiting for it to be hit. You should be able to look up the stack and see where in your code you're triggering the issue, and then inspect the value you're passing into NSUserDefaults.

Basically, it's very unlikely that you're actually passing a valid string in, there's likely a bug in your code that's causing something else to be there.

(Edit to add the idea I had in a comment)

Oh, one other idea: You can use the function CFPropertyListIsValid to determine if an object is a valid property list object. That could let you catch it earlier, in your code, and see what's going on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top