NSMutableArray insertObject:atIndex

时间:2012-06-06 10:06:32

标签: iphone objective-c nsmutablearray

我遇到NSMutableArray问题。我想创建一个填充mutableArray的循环,最初为空,但Xcode生成两个错误:“从不兼容的类型'void'”分配给“NSMutableArray”,“void值不应该忽略,因为它应该是”。 这是代码:

NSMutableArray * arrayToFill =[[NSMutableArray alloc]init];
int i=0;
while (i<4){
    NSDictionary * dictionary =[[NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1",@"value2",@"key2", nil];
    arrayToFill =[arrayToFill insertObject:dictionary atIndex:i];
    i++;
 }

3 个答案:

答案 0 :(得分:2)

更改行:

arrayToFill =[arrayToFill insertObject:dictionary atIndex:i];

[arrayToFill insertObject:dictionary atIndex:i];

您无需再次分配,只需调用insert方法

即可

答案 1 :(得分:1)

只需删除arrayToFill =

即可
 NSMutableArray * arrayToFill = [NSMutableArray array];
    for (int i = 0; i < 4; i++){
        NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"value1",  @"key1", @"value2", @"key2", nil];
        [arrayToFill insertObject: dictionary atIndex: i ];
     }

答案 2 :(得分:1)

将第5行更改为:

[arrayToFill insertObject:dictionary atIndex:i];

您无需重新分配arrayToFill