0

I can retrieve data I need from API, but then I can't change values of NSMutableArray. Here's the data I get from API:

{
favorites =     (
            {
        id = 766;
        imageUrl = "766.jpg";
        name = "Ground Beef 93% Lean 1LB PKG";
        price = "5.99";
        qty = 5;
        sale = "<null>";
        switch = 1;
    },
            {
        id = 3270;
        imageUrl = "3270.jpg";
        name = "Arnold Premium Italian Bread 20oz PKG";
        price = "5.39";
        qty = 5;
        sale = "<null>";
        switch = 1;
    },...
);
success = true;
}

I declare favorites property in the interface part :

@property (nonatomic, retain) NSMutableArray *favorites;

I store the data in NSMutableArray like this :

NSError *myError = nil;
NSMutableArray *response = [[NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError] mutableCopy];

Then I store favorite products in another NSMutableArray object :

NSString *success = [response valueForKey:@"success"];
if([success isEqualToString:@"true"]) {
    self.favorites = [response valueForKey:@"favorites"];
}

With this data I can display all images, prices, etc. in the table. Problem is when I want to update values in self.favorites mutable array:

[self.favorites[indexPath.row] setObject:@"0" forKey:@"switch"];

I get this error : -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object. I even tried to add mutableCopy when assigning array to favorites :

self.favorites = [[response valueForKey:@"favorites"] mutableCopy];

but that did not work either.

2
  • 1
    Your array is mutable, but the dictionaries it contains are still immutable. Commented Jul 6, 2014 at 17:12
  • Like @rmaddy said. I need NSJSONReadingMutableContainers, that's what makes the dictionaries mutable. Commented Jul 6, 2014 at 17:35

1 Answer 1

1

You want NSJSONReadingMutableContainers, not NSJSONReadingMutableLeaves.

Using NSJSONReadingMutableLeaves gives you mutable strings and other mutable objects inside the containers. Not what you need here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.