0

我对 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++;
 }
4

3 回答 3

2

换行:

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

[arrayToFill insertObject:dictionary atIndex:i];

不需要再次赋值,调用insert方法即可

于 2012-06-06T10:08:42.457 回答
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 ];
     }
于 2012-06-06T10:08:56.107 回答
1

将第 5 行更改为:

[arrayToFill insertObject:dictionary atIndex:i];

您不必重新分配 arrayToFill

于 2012-06-06T10:10:46.940 回答