简体   繁体   English

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

[英][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我得到 JSON 数据并将其存储在“NSUserDefaults”中,然后我试图检索以从字典中添加或删除对象,但是我收到错误

unrecognized selector sent to instance无法识别的选择器发送到实例

when adding to the NSMutableDictionary添加到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 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 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];
    }
}

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.还要记住,在您的应用程序第一次运行时, objects将为 null。 So account for that too.所以也要考虑到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [__NSDictionaryI setObject:forKey:]:无法识别的选择器已发送到实例 - [__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance -[__ NSDictionaryI setObject:forKey:]:无法识别的选择器已发送到实例0x91da0f0 - -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x91da0f0 -[__ NSDictionaryI arrayForKey:]:无法识别的选择器已发送到实例 - -[__NSDictionaryI arrayForKey:]: unrecognized selector sent to instance -[__ NSDictionaryI rangeOfString:options:]:无法识别的选择器已发送到实例 - -[__NSDictionaryI rangeOfString:options:]: unrecognized selector sent to instance [__NSDictionaryI个字节]:无法识别的选择器已发送到实例 - [__NSDictionaryI bytes]: unrecognized selector sent to instance __NSDictionaryI setObject:forKey:崩溃 - __NSDictionaryI setObject:forKey: Crash &#39; - [__ NSDictionaryI length]:无法识别的选择器发送到实例&#39; - 试图找出原因 - '-[__NSDictionaryI length]: unrecognized selector sent to instance' - trying to figure out why “ [__NSDictionaryI内容]:无法识别的选择器已发送到实例”从Custom JsonModel获取数据时 - “[__NSDictionaryI content]: unrecognized selector sent to instance” When getting data from Custom JsonModel 无法识别的选择器发送到实例 - unrecognized selector sent to instance 无法识别的选择器已发送到实例 - Unrecognized Selector Sent to Instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM