我将如何创建多个NSDictionary
使用数组计数的变量?
这基本上是我想到的,但我不确定如何使用 Objective-C 语法来实现它。doesntContainAnother
is an NSArray
。我希望字典的名称使用当前值loopInt
.
int *loopInt = 0;
while (doesntContainAnother.count <= loopInt) {
NSMutableDictionary *[NSString stringWithFormat:@"loopDictionary%i", loopInt] = [[[NSMutableDictionary alloc] init] autorelease];
[NSString stringWithFormat:@"loopDictionary%i", loopInt] = [NSDictionary dictionaryWithObject:[array1 objectAtIndex:loopInt]
forKey:[array2 objectAtIndex:loopInt]];
loopInt = loopInt + 1;
}
创建一个可变数组并循环,直到达到原始数组的计数,创建一个字典并在每次迭代时将其添加到可变数组中。
您的代码应该如下所示。
NSMutableArray *dictionaries = [[NSMutableArray alloc] init];
for (int i = 0; i < doesntContainAnother.count; i++) {
[dictionaries addObject:[NSMutableDictionary dictionaryWithObject:[array1 objectAtIndex:i] forKey:[array2 objectAtIndex:i]]];
}
创建名称末尾带有数字的变量的方法是一种反模式,在 Objective-C 中甚至是不可能的。它相当于一个数组,但更笨重。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)