0

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.

6
  • This is homework, forgot to mention. Commented Feb 25, 2013 at 5:34
  • 1
    Also, the fact that the string is retrieved from an array does not change anything in that regard. The way to test for equality is the same for all strings. Commented Feb 25, 2013 at 7:00
  • OK, thanks. I was thinking that I might have to cast it as string explicitly if it came out of the array. Commented Feb 25, 2013 at 7:28
  • You can do that, but that doesn't change anything about the object at run time, it just increases what the compiler knows about it (at compile time, obviously). Commented Feb 25, 2013 at 8:00
  • Worth noting that arc4random_uniform() returns a u_int32_t (which really ought to be a uint32_t) rather than an int. Also, neither of those types is equivalent to NSUInteger, which -objectAtIndex: and -replaceObjectAtIndex:withObject: expect. Always mind your types. Commented Feb 26, 2013 at 5:15

3 Answers 3

1

to know the exist or not and index of it simply use ContainsObject property over NSArray

[array containsObject:@"text"];
int indexOfObject = [array indexOfObject:@"text"];
Sign up to request clarification or add additional context in comments.

1 Comment

I know that the array contains strings of 3 blank spaces, I just want to know if it is at the specified, random, space. This question is a mess, no one really seems to get what I'm asking specifically since I was not clear. I will probably delete it unless I figure out the answer soon and can salvage it. My problem turns out to be in the creation of the array in the first method, I am doing something wrong there. I want it to be an array of size 100 containing only " " at every index.
1

Use this method for string comparison

   [[_board objectAtIndex:random] isEqualToString:@"  "]

Modified your code. I think this is what you are looking for

-(void)makeChutes: (int) numOfChutes
{
    for(int i = 0; i < numOfChutes ;i++){
        int random = arc4random_uniform(101);
        if ([[_board objectAtIndex:random] isEqualToString:@"  "])
        {
            [_board replaceObjectAtIndex:random withObject:@"C10"];
            i++;
        }
    }
}

EDIT : Solution from what you said in comments

-(void)makeChutes: (int) numOfChutes
{
    int i=0;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '  '"];
    NSArray *filteredArray = [_board filteredArrayUsingPredicate:predicate];
    int arrayCount=[filteredArray count];

    do {
        int random = arc4random_uniform(101);
        if ([[_board objectAtIndex:random] isEqualToString:@"  "])
        {
            [_board replaceObjectAtIndex:random withObject:@"C10"];
            i++;
            if (arrayCount == i) {
                i=numOfChutes;
            }
        }

    } while (i<numOfChutes);
 }

EDIT From the DOCS

isEqualToString: Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.

  • (BOOL)isEqualToString:(NSString *)aString Parameters aString The string with which to compare the receiver. Return Value YES if aString is equivalent to the receiver (if they have the same id or if they are NSOrderedSame in a literal comparison), otherwise NO.

Discussion The comparison uses the canonical representation of strings, which for a particular string is the length of the string plus the Unicode characters that make up the string. When this method compares two strings, if the individual Unicodes are the same, then the strings are equal, regardless of the backing store. “Literal” when applied to string comparison means that various Unicode decomposition rules are not applied and Unicode characters are individually compared. So, for instance, “Ö” represented as the composed character sequence “O” and umlaut would not compare equal to “Ö” represented as one Unicode character.

8 Comments

what are you trying to achieve from this method?
Oh, I'm trying to insert the string "C10" into the argument number of random positions in the array. In my program the argument number is 10, so it will look for 10 "blank" spots in the array and insert "C10".
But this will increment i every iteration. I want it to iterate as many times as it take to find a random index location that is " ", not occupied by another string already. So I only want to increment the i when it finds a space not already used.
so you want to remove all the @" " to @"C10" in the iteration?
I want to find the method argument number of random locations that are " " and replace them with "C10". So if I go to a random index and it is something other than " " I do not want to mess with it. In the case of this program, it is calling this method like this: [cl makeChutes:10];, so I will be needing to find ten blank spaces to replace with "C10".
|
0

My "initBoard" method was broken, causing the "makeChutes" method not to work as expected. Here is the correct initBoard:

-(void) initBoard{
    _board = [[NSMutableArray alloc] initWithCapacity:100];
    for(int i =0; i < 100; i++){
        [_board addObject:@"   "];
    }
}

And the corrected makeChutes:

-(void)makeChutes: (int) numOfChutes{
    //Make argument number of Chutes randomly across the board.
    for(int i = 0; i < numOfChutes;){
        int random = arc4random_uniform(100);//+1 of max index
        if ([[_board objectAtIndex:random] isEqualTo:@"   "]){
            NSString *fString = [NSString stringWithFormat:@"C%d", 10];
            [_board replaceObjectAtIndex:random withObject:fString];
            i++;//Only increments i if 3 blank spaces were at random index.
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.