为从服务器加载的数据将2维数组分配给1维数组

时间:2013-03-25 13:57:20

标签: ios objective-c xcode

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

- (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];
}

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

提前完成

3 个答案:

答案 0 :(得分:1)

假设tableData为空NSMutableArray,请使用

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

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

[tableData replaceObjectAtIndex:i withObject:[[sample objectAtIndex:i]objectAtIndex:0]];

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您要做的事情最好转换为:

[tableData replaceObjectAtIndex:i withObject:[[sample objectAtIndex:i]objectAtIndex:0]];
相关问题