Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jchildress

macrumors member
Original poster
Jul 9, 2008
49
0
for some reason, I can't get a string compare to return true, even though the two strings are identical.

First I tried:
Code:
if (string1 == string2) {
    NSLog(@"Strings Match");
}
This did not work.

Then I tried:
Code:
if ([string1 compare: string2] == YES) {
    NSLog(@"Strings Match");
}
This did not work either. I also tried upper and lower casing the strings before the compare, but that did nothing. I also tried caseInsensitiveCompare:

I logged the two strings to the console before the compare, and they appear identical to each other. I'm beginning to think that maybe there is some difference in the format of the two strings. I'm declaring both of them as NSStrings.

Does anybody have any suggestions?
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
for some reason, I can't get a string compare to return true, even though the two strings are identical.

NSString's compare method doesn't return a BOOL, it returns an NSComparisonResult (basically an int). Have the app check which of the 3 possible results it gets, it might help you solve the problem.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Alternatively, use:

Code:
if([string1 isEqual:string2]) {
 // do something
}

Or if you know for sure that both are NSString objects

Code:
if ([string1 isEqualToString:string2]
{
// Do it
}

I have been told in the fast this is slightly more efficient that calling isEqual...
 

Ron C

macrumors member
Jul 18, 2008
61
0
Chicago-area
for some reason, I can't get a string compare to return true, even though the two strings are identical.
NSString's compare method doesn't return a BOOL, it returns an NSComparisonResult (basically an int). Have the app check which of the 3 possible results it gets, it might help you solve the problem.

I too am a Obj-C newbie, but the compare method sounds a lot like C's strcmp(str1,str2), which returns one of three values -
  • a value less than 0 if str1 is "less than" str2
  • 0 if str1 equals str2
  • a value greater than 0 if str1 is "greater than" str2
Looking at the NSString documentation for 2 seconds shows that this is indeed the case. The corresponding values are:
  • NSOrderedAscending - The left operand is smaller than the right operand.
  • NSOrderedSame - The two operands are equal.
  • NSOrderedDescending - The left operand is greater than the right operand.

(this lurking obj-c newbie learns a lot from these kinds of posts, so thanks to admanimal for showing patience :D)

Ron C.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
A tip if in Xcode: command-click a method/class/variable, etc and you'll be taken to its definition (or implementation). Especially helpful for quickly glancing at a method to see what arguments it takes and what it returns.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.