How do I properly compare a string that I am retrieving from an NSArray to a literal string? So far, I have filled an NSArray up with three blank spaces, " " in one method and now I am trying to replace 10 random indexes in the NSArray with the string "C10", but I don't want to replace what is there unless it is " " still.
Here I created the array of size 100 and filled each spot with 3 blank spaces.
-(void) initBoard{
_board = [board initWithCapacity: 100];
for(int i =0; i < 100; i++){
[_board addObject:@" "];
}
}
Here is the method that I'm having problems with the equality comparison.
-(void)makeChutes: (int) numOfChutes {
//Make argument number of Chutes randomly across the board.
for(int i = 0; i < numOfChutes || i>(-100);){
int random = arc4random_uniform(101);
if ([[_board objectAtIndex:random] isEqual:@" "]) {
NSString *fString = [NSString stringWithFormat:@"C%d", 10];
[_board replaceObjectAtIndex:random withObject:fString];
i++;//Only increments i if 3 blank spaces were at random index..
}
else{//Used to attempt to stop my accidental infinite loop.
i--;
NSLog(@"I, loop, ran %i times.", i);//Attempt failed:-(
}
}
}
I know the above code is a mess. In an attempt to stop the looping I made the i decrement every time it did not meet the for condition and then added an OR condition to my for loop, using ||, to try and stop it after 100 cycles. For some reason the || condition does not stop it from looping even while i is well south of -100.
My first question is how do I properly compare the string stored in the array at index "random" with the literal string of 3 blank spaces? I also tried the method isEqualToString, but it worked the same.
Secondly and less importantly, since I don't need it now, how do I properly code a bi-conditional for loop? I don't get any errors or warnings from Xcode with my for loop syntax and it compiles and runs, so I don't really get why it ignores my second conditions and keeps iterating even while i is < -100.
arc4random_uniform()returns au_int32_t(which really ought to be auint32_t) rather than anint. Also, neither of those types is equivalent toNSUInteger, which-objectAtIndex:and-replaceObjectAtIndex:withObject:expect. Always mind your types.