21

I want to check weather a NSString is null or not. Im assigning from an JSON array. After assigning that string value is <null>. Now I want to check this string is null or not. So I put like this

if (myStringAuthID==nil) but this if statement always false. How I can check a string for null. Please help me

Thanks

6

5 Answers 5

69

Like that:

[myString isEqual: [NSNull null]];
Sign up to request clarification or add additional context in comments.

3 Comments

No need to use isEqual. == works just fine, since [NSNull null] is a singleton.
Create a category on NSString and include this method: + (BOOL)isEmpty:(NSString *)string {return !string || [string isEqual:[NSNull null]] || [string isEqualToString:@""];}
if (myString == (NSString*)[NSNull null]) if you do not cast it then there is a warning just pointing that out. The pointers are different types.
10

There are three possible interpretations of "null" NSString:

  1. someStringPtr == nil
  2. (id)someStringPtr == [NSNull null]
  3. someStringPtr.length == 0

If you may have the possibility of all 3, the 3rd check subsumes the first, but I don't know of a simple check for all three.

In general, JSON will return [NSNull null] for a null JSON value, but some kits may return @"" (length == 0) instead. nil will never be used in iOS since it can't be placed in arrays/dictionaries.

Comments

1

Try if(myString == [NSNull null]). That should evaluate it properly.

8 Comments

when I type like that there was a warning saying comparison between distinct pointer types (NSString and NSNull) is it ok?
try you've try with my code?
This is not the best approach. You should use isEqual
Both have been fine for me, I've tried. The == comparison operator checks to see if they're the same object, and as NSNull null is always the same object it should always work (unless Apple subtly changes something).
@Bot not totally true, NSNull is a singleton that gives you always the same pointer, so if myString pointer points to [NSNull null] is safe and also faster to make an comparison check between pointer values.
|
1

I think that is best if you check before cast it to an NSString or whatever, you have different options, the above are correct, but I prefer this:

id NilOrValue(id aValue) {
  if ((NSNull *)aValue == [NSNull null]) {
    return nil;
  }
  else {
    return aValue;
  }
}

Using this snippet (pay attention that is a C function) before passing the value to a pointer you can safely pass a value or nil if the value in NSNull. Passing nil is great, because if you send a message to a nil object, it doesn't throw an exception. You can also check for class type with -isKindOfClass.

Comments

1

Here is part of a string category I created:

@interface NSString (Enhancements)

+(BOOL)isNullOrEmpty:(NSString *)inString;

@end

@implementation NSString (Enhancements)

+(BOOL)isNullOrEmpty:(NSString *)inString
{
    BOOL retVal = YES;

    if( inString != nil )
    {
        if( [inString isKindOfClass:[NSString class]] )
        {
            retVal = inString.length == 0;
        }    
        else
        {
            NSLog(@"isNullOrEmpty, value not a string");
        }
    }
    return retVal;
}

@end

1 Comment

inString != nil is a comparison between a pointer and an integer, means this is not recommended

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.