Will M.
Will M.

Reputation: 1854

Objects in NSMutableArray are all null

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SudokuCell.png"]];
NSLog(@"%@", image);
[[self.blocks objectAtIndex:row] setObject:image atIndex:col];
NSLog(@"%@", [[self.blocks objectAtIndex:row] objectAtIndex:col]);

Is the code that I'm running. The first NSLog prints a UIImageView object, and the second NSLog prints (null). Why does the second NSLog not print the same thing as the first one?

The array self.blocks is initialized as such:

self.blocks = [[NSMutableArray alloc] initWithCapacity: 9];
for (int i = 0; i < 9; i++)
{
    [self.blocks addObject:[[NSMutableArray alloc]initWithCapacity:9]];
}

Upvotes: 0

Views: 237

Answers (1)

DrummerB
DrummerB

Reputation: 40211

NSMutableArray doesn't have a setObject:atIndex: method. You should use insertObject:atIndex: instead. Note, that the index can't be higher than the array's count (not the capacity) eg. it won't automatically fill up your array with "empty" values.

Upvotes: 2

Related Questions