RileyDev
RileyDev

Reputation: 2515

[NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

I get JSON data and store it in `NSUserDefaults' which then I am trying to retrieve to add or remove objects from the dictionary, however I get the error

unrecognized selector sent to instance

when adding to the NSMutableDictionary

 // new dict
    NSMutableDictionary *userTranslations = [NSMutableDictionary new];

    for (NSString *key in keys) {
        [userTranslations setObject:dicky[key] forKey:key];
    };

 // then add that dictionary as the key for uid
    [artworkTranslations setObject:userTranslations forKey:uid];

JSON Data

{
  people =     {
    "title" = "Hello";
    "details" = "Amet justo donec enim diam vulputate.<br><br>Ut etiam sit amet nisl. Mattis vulputate enim nulla aliquet porttitor.<br><br>Euismod quis viverra nibh cras pulvinar mattis nunc sed blandit. Dis parturient montes nascetur ridiculus mus.";
  };
}

NSUserDefaults

Objects: {
  9ddfdd2d652c742 = {
      first =     {
        "title" = "Hello";
        "details" = "Amet justo donec enim diam vulputate.<br><br>Ut etiam sit amet nisl. Mattis vulputate enim nulla aliquet porttitor.<br><br>Euismod quis viverra nibh cras pulvinar mattis nunc sed blandit. Dis parturient montes nascetur ridiculus mus.";
      };
  };
}

Full Code

NSDictionary * jsonData = [data objectForKey:@"people"];

if (jsonData) {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *objects = [defaults objectForKey:@"Objects"];
    NSArray *keys = [jsonData allKeys];

    if (objects) {

        // get people from UD
        NSMutableDictionary *people = [objects objectForKey:uid];

        if (people) {
            NSLog(@"there ARE in people in dictionary from UD");

            //get uid of person key and if exists then remove
            [objects removeObjectForKey:uid]; // error here

            // new dict
            NSMutableDictionary *user = [NSMutableDictionary new];
            for (NSString *key in keys) {
                 [users setObject:jsonData[key] forKey:key]; // Crash Here
            };

            // then add that dictionary as the key for uid
           [people setObject:user forKey:uid];

        } else {
           NSLog(@"NO people dictionary in objects from UD");
            NSMutableDictionary *users = [NSMutableDictionary new];
            NSMutableDictionary *people = [NSMutableDictionary new];

            for (id key in keys) {
               NSMutableDictionary *person = [jsonData objectForKey:key];
               [people setObject:person forKey:key]; // Crash here
           };

            [user setObject:people forKey:uid];
            [objects setObject:users forKey:uid];
            [defaults setObject:objects forKey:@"Objects"];
        }

        [defaults synchronize];
    } else if (!objects){
        NSLog(@"People NOT FOUND in UserDefaults");
        NSMutableDictionary *people = [NSMutableDictionary new];
        NSMutableDictionary *user = [NSMutableDictionary new];

        for (NSString *key in keys) {
            [users setObject:jsonData[key] forKey:key];
        };

        [people setObject:user forKey:uid];
        [defaults setObject:people forKey:@"Objects"];
        [defaults synchronize];
    }
}

Upvotes: 0

Views: 1338

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Your problem is on the line:

NSMutableDictionary *objects = [defaults objectForKey:@"Objects"];

You can't get a mutable collection from user defaults. You need to make it mutable:

NSMutableDictionary *objects = [[defaults objectForKey:@"Objects"] mutableCopy];

Also keep in mind that objects will be null the first time your app runs. So account for that too.

Upvotes: 6

Related Questions