Ortensia C.
Ortensia C.

Reputation: 4716

NSMutableArray insertObject: atIndex

I have a problem with a NSMutableArray. I want to create a loop that fill an mutableArray initially empty but Xcode generates two error: "Assigning to "NSMutableArray" from incompatible type 'void'", "void value not ignored as it ought to be". This is the code:

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++;
 }

Upvotes: 0

Views: 1941

Answers (3)

greenLizard
greenLizard

Reputation: 2346

Change 5th line to this:

[arrayToFill insertObject:dictionary atIndex:i];

You don't have to reassign the arrayToFill

Upvotes: 1

CarlJ
CarlJ

Reputation: 9481

simply remove the 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 ];
     }

Upvotes: 1

Ian L
Ian L

Reputation: 5591

Change the line:

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

to

[arrayToFill insertObject:dictionary atIndex:i];

You don't need to assign it again, just call the insert method

Upvotes: 2

Related Questions