Testing for nil in Objective-C -- if(x != nil) vs if(x)

Objective CSyntax

Objective C Problem Overview


Most of the examples I found on the net write this:

if(x != nil)
    // ...

Is there any problems with this?

if(x)
    // ...

I tried both in a simple program and couldn't found any difference.

Objective C Solutions


Solution 1 - Objective C

In Objective-C, nil is defined as a value called __DARWIN_NULL, which essentially evaluates to 0 or false in if-statements. Therefore, writing if (x == nil) is the same as writing if (!x) and writing if (x != nil) is equal to if (x) (since comparing to false creates a negation, and comparing to true keeps the condition the same).


You can write your code either way, and it really depends on which you think is more readable. I find if (x) to make more sense, but it depends on your style.

It's like comparing if (someCondition == true) versus if (someCondition).
It all depends on you, and who's going to be reading the code.


Edit: As Yuji correctly mentions, since Objective-C is a superset of C, any condition that evaluates to a value other than 0 is considered to be true, and therefore, if someCondition in the example above were to evaluate to an integer value of, say, -1, comparing it to true would result in false, and the if-statement would not be evaluated. Something to be aware of.

Solution 2 - Objective C

Both

if (x != nil)

and

if ( x )

are equivalent, so pick the variant that in your opinion makes your code more readable for you (and others who will read and support your code)

Solution 3 - Objective C

Both are the same and this is a style question and it boils down to whether you prefer:

  1. if (something) { ... }

versus 2. if (something != nothing) { ... }

I have always found #1 more clear but #2 is used extensively in documentation and hence the field so it is better to both know both forms and adapt to what a project uses and be stylistically consistent.

Solution 4 - Objective C

The best and safe way to check nil is
Make a common method, and add all these null :

+ (NSString *)trimWhiteSpaceAndNewLine:(NSString *)string {
    NSString *stringSource = [NSString stringWithFormat:@"%@",string];
    if ([stringSource isEqualToString:@"(null)"]) {
        stringSource = @"";
        return stringSource;
    }
    if ([stringSource isEqualToString:@"<null>"]) {
        stringSource = @"";
        return stringSource;
    }
    if ([stringSource isEqualToString:@"<nil>"]) {
        stringSource = @"";
        return stringSource;
    }
    if ([stringSource isKindOfClass:[NSNull class]]) {
        stringSource = @"";
        return stringSource;
    }
    if ([stringSource isEqualToString:@""]) {
        stringSource = @"";
        return stringSource;
    }
    if (stringSource == nil) {
        stringSource = @"";
        return stringSource;
    }
    NSString *stringFinal = [stringSource stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return stringFinal;
}  

And check

NSString *strUuid = [Common trimWhiteSpaceAndNewLine:[dict valueForKeyPath:@"detail.uuid"]];
        if (![strUuid isEqualToString:@""]) {
            // do your stuff
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkizzx2View Question on Stackoverflow
Solution 1 - Objective CItai FerberView Answer on Stackoverflow
Solution 2 - Objective CVladimirView Answer on Stackoverflow
Solution 3 - Objective CDulio DenisView Answer on Stackoverflow
Solution 4 - Objective CMihir OzaView Answer on Stackoverflow