How do I test if a string is empty in Objective-C?
Marc's answer is correct. But I'll take this opportunity to include a pointer to Wil Shipley's generalized isEmpty
, which he shared on his blog:
static inline BOOL IsEmpty(id thing) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
You can check if [string length] == 0
. This will check if it's a valid but empty string (@"") as well as if it's nil, since calling length
on nil will also return 0.