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

Ril*_*Dev 0 objective-c nsdictionary ios

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

无法识别的选择器发送到实例

当添加到 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];
Run Code Online (Sandbox Code Playgroud)

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.";
  };
}
Run Code Online (Sandbox Code Playgroud)

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.";
      };
  };
}
Run Code Online (Sandbox Code Playgroud)

完整代码

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];
    }
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 6

你的问题上线了:

NSMutableDictionary *objects = [defaults objectForKey:@"Objects"];
Run Code Online (Sandbox Code Playgroud)

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

NSMutableDictionary *objects = [[defaults objectForKey:@"Objects"] mutableCopy];
Run Code Online (Sandbox Code Playgroud)

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