String comparison in Objective-C

ingh.am picture ingh.am · Apr 7, 2010 · Viewed 117.9k times · Source

I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a string containing a GUID and when I attempt to compare this with another string I get some strange results.

Why would this not fire? Surely the two strings are a match?

NSString *myString = @"hello world";

if(myString == @"hello world")
    return;

Answer

jlehr picture jlehr · Apr 7, 2010

Use the -isEqualToString: method to compare the value of two strings. Using the C == operator will simply compare the addresses of the objects.

if ([category isEqualToString:@"Some String"])
{
    // Do stuff...
}