【发布时间】:2013-08-30 14:22:21
【问题描述】:
尝试在 NSMutableArray 的 NSMutableDictionary 中设置单个键/值对时:
[[self.items objectAtIndex:i] setValue:@"value" forKey:@"key"];
self.items 是 NSMutableArray,它有一个 NSMutableDictionaries 列表
它没有设置为单个对象,而是将其设置为列表中的所有字典。
我以前用过这种方法。但我不知道在这种情况下发生了什么。
我知道 NSArray 有 setValue:@"value" forKey:@"key 方法,但在这种情况下我使用的是 NSMutableArray
这里有更多代码块来帮助澄清我的情况:
-(void)setItem:(id)sender
{
for (CellView *cell in self.CollectionView.visibleCells)
{
NSIndexPath *indexPath = [self.CollectionView indexPathForCell:cell];
int i = (indexPath.section * (mainItems.count)/3+ indexPath.row);
if (((UIButton *)sender).tag == i)
{
[[self.items objectAtIndex:i] setValue:@"value" forKey:@"key"];
}
}
}
【问题讨论】:
-
验证
[self.items objectAtIndex:i]是否真的返回字典 -
如果 objectAtIndex:i 是一个可变 dict 并且 value 永远不会是 nil,那么你应该使用 setObject:forValue: setValue 还有很多其他的事情
标签: ios objective-c nsmutablearray