How to test for a (null) string in Objective C

I wanted to test if a string has a value when it’s returned from Core Data. So I thought, the right way to do this was to see if it’s equal to nothing, like so:

if ([myString isEqualToString:@""]) {
    // should be empty... but may not be
}

But to my surprise this didn’t work – because my string was (null) rather than “empty” or nil, which in Objective C are not the same thing.

Instead we can test for the length of this string, and if it returns zero, then we’re sure it’s empty:

    if ([myString length] == 0) {
        // definitely empty!
    }




Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.