有没有办法复制数组中 NSDictionaries 的数量?
Is there a way to duplicate the number of NSDictionaries in an array?
目前我的 table 视图中有这个 NSDictionary
数组,但是当 table 视图向下滚动时,我需要复制数组中的词典数量。有没有办法实现这一点,并指定或知道字典名称是什么?
NSMutableDictionary *d0 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d1 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d3 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d4 = [[NSMutableDictionary alloc] init];
objects = [NSMutableArray arrayWithObjects:d0, d1, d2, d3, d4, nil];
首先,在循环中创建字典似乎更容易:
NSMutableArray *objects = [NSMutableArray new];
for( int i = 0; i < 5; i++ )
{
[objects addObject:[NSMutableDictionary new]];
}
如果你想 "duplicate" 字典,你也可以用循环来实现:
for( NSMutableDictionary *dictionary in [objects copy])
{
[objects addObject:[dictionary mutableCopy]];
}
但我无法摆脱你的整个方法是错误的感觉。这背后的想法是什么?
目前我的 table 视图中有这个 NSDictionary
数组,但是当 table 视图向下滚动时,我需要复制数组中的词典数量。有没有办法实现这一点,并指定或知道字典名称是什么?
NSMutableDictionary *d0 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d1 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d3 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *d4 = [[NSMutableDictionary alloc] init];
objects = [NSMutableArray arrayWithObjects:d0, d1, d2, d3, d4, nil];
首先,在循环中创建字典似乎更容易:
NSMutableArray *objects = [NSMutableArray new];
for( int i = 0; i < 5; i++ )
{
[objects addObject:[NSMutableDictionary new]];
}
如果你想 "duplicate" 字典,你也可以用循环来实现:
for( NSMutableDictionary *dictionary in [objects copy])
{
[objects addObject:[dictionary mutableCopy]];
}
但我无法摆脱你的整个方法是错误的感觉。这背后的想法是什么?