开发者

how to edit an NSMutableArray data?

开发者 https://www.devze.com 2023-03-31 11:46 出处:网络
I have this NSMutableArray sections = [[NSMutableArray alloc] init]; [sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@\"Electricity\",@\"Type\",@\"0\",@\"Count\", nil]];

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开发者_StackOverflow社区 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?


[[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).

0

精彩评论

暂无评论...
验证码 换一张
取 消