Can I simply use
if(myString == nil)
For some reason a string that I know is null, is failing this statement.
You can implicitly check for nil
(allocated, but not initialized) with this:
if (!myString) {
//do something
}
If myString
was assigned from a dictionary or array, you may also wish to check for NSNULL
like this:
if ([myString isEqual:[NSNull null]]) {
//do something
}
Finally (as Sophie Alpert mentioned), you can check for empty strings (an empty value):
if ([myString length] == 0) {
//do something
}
Often, you may want to consolidate the expressions:
if (!myString || [myString length] == 0) {
//do something
}
Notice length = 0 doesn't necessary mean it's nil
NSString *test1 = @"";
NSString *test2 = nil;
They are not the same. Although both the length are 0.