我有一个for循环,但无法从循环外部访问变量:
for (int i = 0, j = 0, k = 0, l = 0; i < [array1 count] && j < [array2 count] && k < [array3 count] && l < [array4 count]; i++ && j++ && k++ && l++)
{
NSDictionary *_myDict = [NSDictionary dictionaryWithObjectsAndKeys:[array1 objectAtIndex:i], @"Apples", [array2 objectAtIndex:j], @"Oranges", [array3 objectAtIndex:k], @"Grapes", [array4 objectAtIndex:l], @"Plums", nil];
}
从内部访问它。
我尝试使用单例和实例方法,它仍然无法访问并始终返回nil。
我很想拥有它的全局变量,但不能在循环中声明它。
答案 0 :(得分:0)
NSMutableArray *rows = [NSMutableArray array];
for (int i=0, j=0, k=0, l=0;
i < [array1 count] && j < [array2 count] && k < [array3 count] && l < [array4 count];
i++, j++, k++, l++)
{
NSMutableDictionary *row = [NSMutableDictionary dictionary];
[row setObject:[array1 objectAtIndex:i] forKey:@"Apples"];
[row setObject:[array2 objectAtIndex:j] forKey:@"Oranges"];
[row setObject:[array3 objectAtIndex:k] forKey:@"Grapes"];
[row setObject:[array4 objectAtIndex:l] forKey:@"Plums"];
[rows addObject:row];
}
现在将arrayController
连接到rows
数组,将其行类设置为NSMutableDictionary,并在列绑定中使用@“Apples”等。希望能解决它。