[NSDictionaryI setObject:forKey:]: 无法识别的选择器发送到实例

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

我得到 JSON 数据并将其存储在“NSUserDefaults”中,然后我试图检索以从字典中添加或删除对象,但是我收到错误

unrecognized selector sent to instance

添加到 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数据

{
  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.";
      };
  };
}

完整代码

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];
    }
}

您的问题在线:

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

您无法从用户默认值中获取可变集合。你需要让它可变:

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

另请记住,objects 在您的应用首次运行时将为 null。所以也要考虑到这一点。