I have a string
NSString hi;
and I don’t know what values will get initialized to it. Could be nil, could be empty string, could be anything.
Are there any advantages to using
if (![hi length])
vs
if (![hi isEqualToString:@""])
It seems like both cases return the same values for empty string, nil, and any other type of string. I would guess length is better because it’s more efficient. It just returns a variable, where as isEqualToString has to do a comparison.
They don’t do the same thing.
[hi length]will return 0 for nil or an empty string and nonzero for any other string.[hi isEqualToString:@""]will return 1 whenhiis an empty string and 0 whenhiis nil or any non-empty string.In other words, the only value of
hifor which the two lines of code give the same result isnil.You probably wanted the behavior of option #1 (treating either nil or an empty string as “blank” and any other value as “not blank”), so that would be the one to use.