Kavi Patel
Kavi Patel

Reputation: 21

Change value in NSDictionary

I have data that comes from server and i save it in NSMutableDic but when i change value in that NSMutabelDic it gives me error that i can't change in NSDic! This is error :

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

This is my sample server data :

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

So i have to change value of question_ans & question_ans_comments so as when i change it i get error as i mention above. This is the way i already applied:

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

i don't have index in sequence like how i replace that key object!! Is there any easy way that i can apply here ?

This is the way i reset value :

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

I also tried this :

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

Giving same error.

Upvotes: 0

Views: 861

Answers (4)

Bhavesh Dhaduk
Bhavesh Dhaduk

Reputation: 1906

Try this to change the value of question_ans & 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);

Upvotes: 0

Dipak kasodariya
Dipak kasodariya

Reputation: 36

Download these categories classes and add them into your project with:

#import "NSDictionary+FRMutableDeepCopy.h"

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

After that you can update nested dictionary.

Upvotes: 2

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

The response which you get from the server is static. So its come in NSDictionary. You need to alloc your own NSMutableDictionary and you have add all value into it.

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

Now you can use dicYourData anywhere and make it updatable.

Upvotes: 0

Pratik Jamariya
Pratik Jamariya

Reputation: 820

try using this method

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

for example

NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary:oldDict];

Edit: Do something like this

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

Upvotes: 0

Related Questions