0

I tried to save an NSDictionary in NSUSerDefaults, but I get the following error: Attempt to insert non-property value

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

 if(defaults) {
     [defaults setBool: YES forKey: @"disableGetStarted"];
     [defaults setObject: [json mutableCopy]  forKey: @"user"];
     [defaults synchronize];

     NSLog(@"defaults %@", [defaults objectForKey: @"user"]);
 }

Where json is an NSDictionary.

What can I do?

8
  • I would not recommend to save a massive NSDictionary as user defaults. the user default is not for storing huge amount data, for this there is the Documents folder.
    – holex
    Aug 22, 2013 at 14:54
  • he didn't say the dictionary is massive Aug 22, 2013 at 14:55
  • 2
    @AndreiStanescu, ...and exactly that is the reason why I assume the dictionary is massive.
    – holex
    Aug 22, 2013 at 14:57
  • Did you check this NSLog(@"class name == %@",[json class]); ?? Aug 22, 2013 at 14:58
  • 1
    Why are you setting it to a mutable copy? When it comes out, it will not be mutable.
    – Kevin
    Aug 22, 2013 at 15:28

4 Answers 4

2

json may be a dictionary but all of the contents of the dictionary must be legal values to be stored in user defaults. All must be instances of: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

2
  • ...and the mutable version of them (if they have).
    – holex
    Aug 22, 2013 at 14:56
  • 1
    @holex which being subclasses are included in such enumeration :) Aug 22, 2013 at 15:35
2

If you only store standard objects inside the dictionary like NSData, NSString, NSNumber, NSDate, NSArray, NSDictionary or a combination of them you don't have to do anything special.

However, if you have instances of custom objects in it (i.e. classes that you've created) you first need to convert it into a compatible type (e.g. NSData). You can do this using the code below:

NSData* data=[NSKeyedArchiver archivedDataWithRootObject:json];
[NSUserDefaults standardUserDefaults] setObject:data forKey:@"user"]

For this method to work, you ned to implement these 2 methods in the custom classes you are trying to save:

- (id)initWithCoder:(NSCoder*)coder
- (void)encodeWithCoder:(NSCoder*)coder;

To get the dictionary back from NSUserDefaults (decode) you can use:

NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
NSDictionary* json = [NSKeyedUnarchiver unarchiveObjectWithData:data];

EDIT

To check if your json object contains any [NSNull null] values, add this piece of code before you are making your insert into NSUserDefaults

for (id val in [json allValues])
{
    if ([val isKindOfClass:[NSNull class]])
    {
        NSLog(@"This bad! NSNull should not be in the dictionary");
        break;
    }
}

If you get any This is bad... messages in the console, then you have 2 choices. 1. Use the archiving/unarchiving method I described above 2. Replace the NSNull objects from the dictionary with other values (e.g. empty strings) if this does not break your code.

2
  • where do I put initWithCoder and encodeWithCoder? The items in the nsdictionary are NSStrings. thanks. Aug 22, 2013 at 19:12
  • if the items are NSString then you shouldn't worry about it. Those 2 methods are used only if you store custom classes that you created. Like others suggested, check that json does not contain any [NSNull null] values. I will edit my post with an example code to do this. Aug 22, 2013 at 19:32
0

One or more of the objects in your dictionary are not NSString, NSArray, NSDictionary, NSDate or NSData, the only objects that may be present. This code may help, it gives you more details why your dictionary is not valid:

- (BOOL)isValidPropertyList:(id)object {

    //
    // Check if `object` is a valid property list.
    //
    // # Return Value
    // `YES` if the receiver is a valid property list, `NO` otherwise.
    // 
    // # Discussion
    // Intended to be called on NSArray and NSDictionary object to verify if the
    // receiver is a valid property list. Will emit a description of any
    // detected error.
    // 
    NSData      *xmlData;
    NSString    *error;

    xmlData=[NSPropertyListSerialization
        dataFromPropertyList:object
        format:NSPropertyListXMLFormat_v1_0
        errorDescription:&error
    ];
    if(xmlData)
    {
        return YES;
    }
    else
    {
        NSLog(@"Tested object is not a valid property list: %@",error);
    }

    return NO;
}
-1
NSDictionary *dict = [[NSDictionary  alloc] init];

dict = json;

[NSUserDefaults standardDefaults] setObject:dict forKey:@"user"];

NSUserDefaults doesn't distinguish between mutable and immutable objects, so when you get it back it'll be immutable. So if you make a mutable dictionary by chance ->

[[NSUserDefaults standardDefaults] setObject:dict forKey:@"user"] mutableCopy];

All objects of the dictionary json must be instances of: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.