views:

4369

answers:

6

I was using this in my iPhone app

if (title == nil) {
    // do something
}

but it throws some exception, and the console shows that the title is "(null)".

So I'm using this now:

if (title == nil || [title isKindOfClass:[NSNull class]]) {
    //do something
}

What is the difference, and what is the best way to determine whether a string is null?

+2  A: 

If that kind of thing does not already exist, you can make an NSString category:

@interface NSString (TrucBiduleChoseAdditions)

- (BOOL)isEmpty;

@end

@implementation NSString (TrucBiduleChoseAdditions)

- (BOOL)isEmpty {
    return self == nil || [@"" isEqualToString:self];
}

@end
Rémy BOURGOIN
That's probably overkill and I don't think it'll solve this problem.
Roger Nolan
The self == nil is pointless - if self was nil, Objective C would not have called you in the first place. [(NSString*)nil isEmpty] would simply return 0/nil/false without ever calling your code.
Peter N Lewis
Actually, further to that, [(NSString*)nil isEmpty] will return false. When writing such Objective C methods, it is better to define them so that nil's 0/nil/false makes sense, so writing the method as hasCharacters would be better. Note that length already works for this, as you can use if ( s.length ) anywhere you would want to use if ( !s.isEmpty ) and s.length will correctly return 0 for the s == nil case.
Peter N Lewis
+4  A: 

Refer to the following related articles on this site:

I think your error is related to something else as you shouldn't need to do the extra checking.

Also see this related question: http://stackoverflow.com/questions/598396/proper-checking-of-nil-sqlite-text-column

TimM
Annoyingly I don't have enough "points" to add a second hyperlink (even to something on this site)see also: - http://stackoverflow.com/questions/598396/proper-checking-of-nil-sqlite-text-column
TimM
...allow me. Although you should be able to edit your own posts from rep 0.
Roger Nolan
Thanks - that was very kind. When I wrote my initial post it said something like new users can only add 1 hyperlink (which seems a bit harsh - especially for cross links in this site).
TimM
A: 

If you want to test against all nil/empty objects (like empty strings or empty arrays/sets) you can use the following:

static inline BOOL IsEmpty(id object) {
    return object == nil
        || ([object respondsToSelector:@selector(length)]
        && [(NSData *) object length] == 0)
        || ([object respondsToSelector:@selector(count)]
        && [(NSArray *) object count] == 0);
}
Diederik Hoogenboom
This does not actually check for object == [NSNull null] which would probably make sense.
Peter N Lewis
+5  A: 

it is just as simple as

if([object length] >0)
{
  // do something
}

remember that in objective C if object is null it returns o as the value.

This will get you both a null string and a 0 length string.

Bluephlame
+5  A: 

As others have pointed out, there are many kinds of "null" under Cocoa/Objective C. But one further thing to note is that [title isKindOfClass:[NSNull class]] is pointlessly complex since [NSNull null] is documented to be a singleton so you can just check for pointer equality. See Topics for Cocoa: Using Null.

So a good test might be:

if (title == [NSNull null] || title.length == 0 ) title = @"Something";

Note how you can use the fact that even if title is nil, title.length will return 0/nil/false, ie 0 in this case, so you do not have to special case it. This is something that people who are new to Objective C have trouble getting used to, especially coming form other languages where messages/method calls to nil crash.

Peter N Lewis
Thanks! However I asked this question in the first place because I was getting exception because the "title" null.
Seymour Cakes
+1  A: 

After trying all the stuff mentioned above below solution worked for me

[text isEqualToString:@"(null)"]

Wondering if there is a full proof method

Yogesh
All of these attempts and the answer at the bottom with zero points is the one that worked for me.
SixOThree