Mahmoud
Mahmoud

Reputation: 757

how to edit an NSMutableArray data?

I have this NSMutableArray

sections = [[NSMutableArray alloc] init];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Electricity",@"Type",@"0",@"Count", nil]];
    [sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Water",@"Type",@"0",@"Count", nil]];
    [sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mobile",@"Type",@"0",@"Count", nil]];
    [sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Internet",@"Type",@"0",@"Count", nil]];
    [sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Fixed Line",@"Type",@"0",@"Count", nil]];

and lets say a user choose to change Water count from 0 to 10 how to do that?

Upvotes: 0

Views: 477

Answers (1)

kpower
kpower

Reputation: 3891

[[sections objectAtIndex:1] setObject:@"10" forKey:@"Count"];

One more thing. When you create objects with alloc+init, you should release them yourself. So for each NSMutableDictionary you have a memory leak (in your code).
Use either [[[NSMutableDictionary alloc] initWithObjectsAndKeys:..., nil] autorelease]; or [NSMutableDictionary dictionaryWithObjectsAndKeys:..., nil]; (of course, you can make something like for (NSMutableDictionary *dict in sections) [dict release]; later, but this looks ugly).

Upvotes: 5

Related Questions