0

我正在尝试将多维数组分配给我的 UITable 视图构造的单维数组。我决定使用循环来节省时间,因为手动添加数组的数据加载非常繁重。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString1 = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];

    NSLog(@"%@",responseString1);

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    id sample = [parser objectWithString:responseString1];

    for (int i = 0; i < [sample count]; i++) {
        [tableData addObject:[[sample objectAtIndex:i]objectAtIndex:1]];
        NSLog(@"%@",[sample objectAtIndex:i]);
        NSLog(@"%@",[tableData objectAtIndex:i]);
    }


    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

现在,'sample' 的 NSLog 反映了数据,但 'tableData' 的 NSLog 反映了 NULL。

提前感谢

4

3 回答 3

1

假设tableData是一个空的NSMutableArray,只需使用

[tableData addObject:[[sample objectAtIndex:i]objectAtIndex:0]];

如果您确实想更改tableData而不是构建它,请使用

[tableData replaceObjectAtIndex:i withObject:[[sample objectAtIndex:i]objectAtIndex:0]];
于 2013-03-25T14:05:58.140 回答
0

NSMutableArray 不是像 c 那样的静态 c 数组id myArray[5];,它就像 c++ veсtor<NSObject *>,所以你不能将元素放入其中[tableData objectAtIndex:i] = ...,你应该使用它的方法[tableData addObject:...](将元素放在数组的末尾)或[tableData insertObject:... atIndex:i];

于 2013-03-25T14:05:38.010 回答
0

您尝试做的事情最好翻译成:

[tableData replaceObjectAtIndex:i withObject:[[sample objectAtIndex:i]objectAtIndex:0]];
于 2013-03-25T14:06:06.707 回答