0

i have a NSMutableArray. In this is the structure of my Array:

{
Distance = 0;
Name = Name1;
Town = Town1;
}
{
Distance = 0;
Name = Name2;
Town = Town2;
}

And now I want to replace "Distance". For example that "Distance = 15".

How can I do this?

1 Answer 1

1

OK, let's say your initial NSMutableArray is called initial (given that it contains NSMutableDictionary objects...

To alter the first element's distance value :

[[initial objectAtIndex:0] setValue:[NSNumber numberWithInt:15] 
                           forKey:@"Distance"];

IF you want to perform the correction for every entry in the array, just do it this way :

for (NSMutableDictionary* entry in initial)
{
     [entry setValue:[NSNumber numberWithInt:15] 
              forKey:@"Distance"];
}
Sign up to request clarification or add additional context in comments.

10 Comments

2013-02-13 08:54:42.591 Heurigen-App[555:11c03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' *** First throw call stack: (0x1848012 0x1178e7e 0x1847deb 0x180e347 0xb919bf 0x6226 0x17af4b 0x17b01f 0x16380b 0x17419b 0x11092d 0x118c6b0 0x2fbefc0 0x2fb333c 0x2fb3150 0x2f310bc 0x2f32227 0x2f328e2 0x1810afe 0x1810a3d 0x17ee7c2 0x17edf44 0x17ede1b 0x17a27e3 0x17a2668 0xc065c 0x26bd 0x25e5) libc++abi.dylib: terminate called throwing an exception (lldb)
@theandrew The error message is pretty self-explanatory, isn't it? As I've already mentioned, the array has to contain instances of NSMutableDictionary. If they're just NSDictionary instances (= IMmutable), how could they be altered?
@theandrew Could you post the code with which you create your initial array?
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
@theandrew Try this : news = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];. It should work.
|

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.