I am trying to create a NSMutableArray of size 100 and then fill every index with the a string of three blank spaces, " "
. I am either creating the array incorrectly or I am returning the objects incorrectly. Here is my method that creates the array:
-(void) initBoard{
_board = [board initWithCapacity:100];
for(int i =0; i < 100; i++){
[_board addObject:@" "];
}
}
This method is not functioning properly. I added an a for loop after the one above that contained NSLog @"@%", [_board objectAtIndex: i];
so that I could verify that there were actually strings comprised of three blank spaces at every index, but all that it would print out in the console was "nil". So far I have tried:
I thought that maybe the addObjectAtIndex
was the problem so I just changed it to addObject
and changed the initWithCapacity
to just init];
to allow the for loop to build the array to size 100.
Then I thought that maybe the problem was with trying to insert the literal string, @" "
(IDK how to make Stackoverflow show my 3 spaces, not just 1), so I added NSString *input = @" ";
right before the for loop, and then changed [_board addObject:@" "];
to `[_board addObject:input;
Any suggestions on how to actually create an NSMutableArray of size 100 and make every spot contain the same string of three blank spaces?
You need to alloc
ate and than init
ialize the mutable array:
-(void) initBoard{
_board = [[NSMutableArray alloc] initWithCapacity:100];
for(int i =0; i < 100; i++){
[_board addObject:@" "];
}
}