views:

10235

answers:

5

How do I test if an NSString is empty in Objective C?

+59  A: 

You can check if [string length] == 0. This will check if it's a valid but empty string (@"") as well as if its nil, since calling length on nil will also return 0.

Marc Charbonneau
+15  A: 

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);
}
Matt G
Thanks for this, I'd forgotten about it.
Abizern
If you want this to be very generalized, one could implement this logic as a category on NSObject instead of using a static method as shown here.
Brad Smith
Oddly, this does not actually check for [NSNull null].
Peter N Lewis
+5  A: 

The first approach is valid, but doesn't work if your string have blank spaces (@" "). So you must to clear this white spaces before test it.

This code clear all the blank spaces on both sides of one string:

[stringObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];

One good idea is create one macro, so you don't have to type this monster line:

#define allTrim( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]

Now you can use:

NSString *emptyString = @"   ";

if ( [allTrim( emptyString ) length] == 0 ) NSLog(@"Is empty!");
SEQOY Development Team
this one works the best for me, thanks!
iWasRobbed
Why use macros when they are not necessary? In this case any sort type safety is sacrificed for no real benefit.
Whisty
Is for simple convenience, if you like to write "[object stringObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]" everytime you want to check if one string is empty, is up to you.
SEQOY Development Team
@SEQOY that would better be served by an `NSString` category that adds a method called `trimmedString` that does exactly what you wrote.
Dave DeLong
Good to known Dave. Thank you.
SEQOY Development Team
+2  A: 

You should better use this category:

@implementation NSString (Empty)

    - (BOOL) empty{
        return ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] == 0);
    }

@end
Muhammad Hassan
That definition would conlude that the string " /r/n /r/n" is empty, when it clearly is not - it contains whitespace. Your function is really:-(BOOL)isWhitespace(NSString*);
JBRWilkinson
A corollary to this. I implemented this category, and there is a twist. If you call this on a nil string, this function is never called, and you get back a NO(or what evaluates to NO) as a return value. Then you think it's not empty...This might work if the name was isFilled or something like that.
Ying
+1  A: 

One of the best solution I ever seen (better than Matt G's one) is this improved inline function I picked up on some Git Hub repo (sorry I forgot) :

// Check if the "thing" pass'd is empty
static inline BOOL isEmpty(id thing) {
    return thing == nil
    || [thing isKindOfClass:[NSNull class]]
    || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}
Rob