Search code examples
objective-cnsstring

String comparison in Objective-C


I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a NSString containing a GUID and when I attempt to compare this with another NSString 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;

Solution

  • 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...
    }