ASH ASH - 10 months ago 55
Objective-C Question

How to insert new key and value based on user input, at index path?

i have an nsmutablearray called orderarray that holds json, and in each dictionary there is the following:


Now based on a user's textfield input i want to add a new key in that dictionary at the index path with a key of "toorder" and a value of "toorder.text"

Here is my current code:

NSMutableDictionary *mutableObject=[NSMutableDictionary dictionaryWithDictionary:[orderarray objectAtIndex:indexPath.row]];
[mutableObject setObject:@"toorder.text" forKey:@"toorder"];
[orderarray replaceObjectAtIndex:indexPath.row withObject:mutableObject];

and i get the error:

[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object

In my h. file i have

NSMutableArray* orderarray;

And then in my m. file:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"urlhere"]];

NSError* error;

orderarray = [NSJSONSerialization JSONObjectWithData:data

dispatch_async(dispatch_get_main_queue(), ^{

if (!error) {


The error message says that orderarray is immutable. You have to provide the option NSJSONReadingMutableContainers to get mutable objects:

orderarray = [NSJSONSerialization JSONObjectWithData:data