我有这个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]];
并且假设用户选择将水数从0更改为10怎么做?
答案 0 :(得分:5)
[[sections objectAtIndex:1] setObject:@"10" forKey:@"Count"];
还有一件事。使用alloc + init创建对象时,应自行释放它们。因此,对于每个NSMutableDictionary
,您的内存泄漏(在您的代码中)
使用[[[NSMutableDictionary alloc] initWithObjectsAndKeys:..., nil] autorelease];
或[NSMutableDictionary dictionaryWithObjectsAndKeys:..., nil];
(当然,您可以稍后制作类似for (NSMutableDictionary *dict in sections) [dict release];
的内容,但这看起来很难看。)