更改NSDictionary中的值

时间:2017-07-06 05:27:10

标签: objective-c json nsdictionary

我有来自服务器的数据,我把它保存在NSMutableDic中但是当我更改NSMutabelDic中的值时,它给出了我在NSDic中无法改变的错误! 这是错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7ae250c0'

这是我的示例服务器数据:

{
    "data": {
        "My_Tool_Box": {
            "Visual Check": [
                {
                    "que_cat_id": "1",
                    "question_id": "1",
                    "question_name": "Power leads",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "2",
                    "question_name": "Power",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "3",
                    "question_name": "Rcd is operational",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "4",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "5",
                    "question_name": "Safety guards if fitted  to machine ARE OPERATIONAL AND ARE NOT DAMAGED",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "6",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "1",
                    "question_id": "7",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                }
            ],
            "Operational checks": [
                {
                    "que_cat_id": "2",
                    "question_id": "8",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "2",
                    "question_id": "9",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                },
                {
                    "que_cat_id": "2",
                    "question_id": "10",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                }
            ],
            "Operating instructions": [
                {
                    "que_cat_id": "3",
                    "question_id": "11",
                    "question_name": "ase to avoid damage",
                    "question_ans": "",
                    "question_ans_comments": "",
                    "question_options": {
                        "1": "Yes",
                        "2": "No",
                        "3": "N/A",
                        "4": "Comment"
                    }
                }
            ]
        }
    },
    "status": "success",
    "message": "Question Listing"
}

所以我必须改变question_ans & question_ans_comments的值,所以当我改变它时,我会得到错误,如上所述。 这是我已经应用的方式:

NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[dataArray objectAtIndex:0];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:@"Don" forKey:@"Name"];
[dataArray replaceObjectAtIndex:0 withObject:newDict];

我没有顺序索引,就像我如何替换那个关键对象!有什么简单的方法可以在这里申请吗?

这是我重置值的方式:

[[[arrayGlobal valueForKey:[arrayAllColumn objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] setObject:@"12" forKey:@"question_ans"];

我也试过这个:

    NSDictionary *oldDict = (NSDictionary *)[[arrayGlobal valueForKey:[arrayAllColumn objectAtIndex:0]] objectAtIndex:0];
    NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary:oldDict];
    [newDict setObject:@"12" forKey:@"question_ans"];
    [[[[arrayGlobal valueForKey:[arrayAllColumn objectAtIndex:0]] objectAtIndex:0] mutableCopy] replaceObjectAtIndex:0 withObject:newDict];

给出同样的错误。

4 个答案:

答案 0 :(得分:2)

下载these categories课程并将其添加到您的项目中:

#import "NSDictionary+FRMutableDeepCopy.h"

NSMutableDictionary *dixDataMutable = [[dixResponse objectForKey:@"data"] mutableDeepCopy];

之后,您可以更新嵌套字典。

答案 1 :(得分:0)

尝试使用此方法

toggled

例如

+ (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType, ObjectType> *)dict;

编辑: 做这样的事情

NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary:oldDict];

答案 2 :(得分:0)

尝试此操作以更改question_ans&amp;的价值question_ans_comments

NSString *jsonString = @"{ \"data\": { \"My_Tool_Box\": { \"Visual Check\": [ { \"que_cat_id\": \"1\", \"question_id\": \"1\", \"question_name\": \"Power leads\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"2\", \"question_name\": \"Power\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"3\", \"question_name\": \"Rcd is operational\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"4\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"5\", \"question_name\": \"Safety guards if fitted to machine ARE OPERATIONAL AND ARE NOT DAMAGED\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"6\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"1\", \"question_id\": \"7\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } } ], \"Operational checks\": [ { \"que_cat_id\": \"2\", \"question_id\": \"8\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"2\", \"question_id\": \"9\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } }, { \"que_cat_id\": \"2\", \"question_id\": \"10\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } } ], \"Operating instructions\": [ { \"que_cat_id\": \"3\", \"question_id\": \"11\", \"question_name\": \"ase to avoid damage\", \"question_ans\": \"\", \"question_ans_comments\": \"\", \"question_options\": { \"1\": \"Yes\", \"2\": \"No\", \"3\": \"N/A\", \"4\": \"Comment\" } } ] } }, \"status\": \"success\", \"message\": \"Question Listing\" }";


NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];

NSMutableDictionary *oldDic = [[[[dic objectForKey:@"data"] objectForKey:@"My_Tool_Box"] objectForKey:@"Visual Check"] objectAtIndex:0];
NSLog(@"oldDic :: %@",oldDic);

[oldDic setObject:@"iOS Dev Answer" forKey:@"question_ans"];
[oldDic setObject:@"iOS Dev Comments" forKey:@"question_ans_comments"];

NSLog(@"dic :: %@",dic);

答案 3 :(得分:0)

您从服务器获得的响应是​​静态的。因此它来自 NSDictionary 。您需要分配自己的 NSMutableDictionary ,并且已将所有值添加到其中。

NSMutableDictionary *dicYourData = [[NSMutableDictionary alloc] initWithDictionary:PASS_YOUR_RESPONSE_DIRECTORY];

现在您可以在任何地方使用 dicYourData 并使其可更新。