Compare two NSStrings

Ohad Regev picture Ohad Regev · Aug 6, 2011 · Viewed 41.6k times · Source

In my app there is a mechanism that requires that at a certain point two NSStrings will be the same to do something; for some reason when I compare the two, even when they are the same, it still doesn't recognize that. The code is something like this:

NSString * aString = [self someMethodThatGetsAString];

NSString * bString;

BOOL areStringsTheSame = NO;

while (areStringsTheSame != YES) {

       bString = [self someMethodThatTakesNSStringsFromAnArrey];
       if (bString == aString) {
             areStringsTheSame = YES;
       { }

I even inserted an NSLog() and made sure that at a certain point they were the same (and as far as I know this is what == stands for...), but still it didn't get into the if to change the BOOL value.

Is there another way to do this comparison? Am I missing something?

Answer

MByD picture MByD · Aug 6, 2011

You can use the method isEqualToString::

if ([bString isEqualToString:aString])

== compares the references (addresses of) the strings, and not the value of the strings.