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

Mirko Catalano
  • 3,850
  • 2
  • 26
  • 39
user2889249
  • 899
  • 2
  • 13
  • 22

5 Answers5

69

Like that:

[myString isEqual: [NSNull null]];
Adam Prax
  • 6,413
  • 3
  • 30
  • 31
Mirko Catalano
  • 3,850
  • 2
  • 26
  • 39
  • No need to use `isEqual`. `==` works just fine, since `[NSNull null]` is a singleton. – Hot Licks Apr 23 '15 at 00:14
  • Create a category on NSString and include this method: ```+ (BOOL)isEmpty:(NSString *)string {return !string || [string isEqual:[NSNull null]] || [string isEqualToString:@""];}``` – jungledev Aug 28 '17 at 15:28
  • 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. – Adam Freeman Mar 05 '19 at 02:18
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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

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

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • when I type like that there was a warning saying comparison between distinct pointer types (NSString and NSNull) is it ok? – user2889249 Oct 23 '13 at 16:19
  • try you've try with my code? – Mirko Catalano Oct 23 '13 at 16:20
  • This is not the best approach. You should use `isEqual` – Bot Oct 23 '13 at 16:22
  • 2
    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). – Nicholas Smith Oct 23 '13 at 16:25
  • 3
    @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. – Andrea Oct 23 '13 at 16:26
  • @Andrea Keep in mind that this relies on an implementation detail. While it may work at the moment, using `isEqual:` is safer and does not depend on an implementation detail. – rmaddy Oct 23 '13 at 17:24
  • @rmaddy - Not an "implementation detail" -- it's in the spec for NSNull. – Hot Licks Oct 23 '13 at 18:19
  • == does not match NSNull to NSString – Code cracker Oct 15 '14 at 10:03
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.

Andrea
  • 26,120
  • 10
  • 85
  • 131
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
JonahGabriel
  • 3,066
  • 2
  • 18
  • 28