NSString contains: <null> and my application crashes

I have an NSString that is populated with some data returned via JSON.

The code works fine under normal circumstances, but there is a case where I will return JSON.

When I check to see if my NSString == nil or == null test is lost.

But the fact that NSString contains the failure of my application.

So it makes sense in Objective-C? Or just need to do a string comparison and see if the string is equal to not zero, and handle it that way.

It bothers me a bit.

Thanks a lot Code

+3
source share
4 answers

<null>is what NSNullreturns for its method -description. You should also check

myString == [NSNull null]

in this case.

: IIRC Objective-C JSON [NSNull null] null JSON, , .

+7

NSString * - NSString.
:

NSString *str;

if (str) {
    // str points to an object
    if ([str length] == 0) {
        // string is empty
    }
} else
    // str points to nothing
}

, NSString stringByTrimmingCharactersInSet.

+2

You can check if null is not.

if ([str isKindOfClass:[NSNull class]]) {
   // str is null.
}
+1
source

I did it as follows:

if([string isKindOfClass:[NSNull class]]) {
    NSLog(@"This is JSON null");
} else {
    NSLog(@"This is a string, do what you wanna do with it");
}
0
source

All Articles