user542584
user542584

Reputation: 2689

What is the difference between if(!variable) and if(variable != nil) iphone

What exactly is the difference between

if(!variable)
   do something

and

if(variable != nil)
    do something

Upvotes: 0

Views: 207

Answers (2)

Joseph Lin
Joseph Lin

Reputation: 3334

They're the exactly the same:

if ( !var ) is equivalent to if ( var != nil )

Upvotes: 1

As noted you have the logic a bit backwards, but outside of that they would work the same.

The main thing to consider is that ( variable != nil ) is a bit more clear as to what you are checking and what type the variable is, since someone may start thinking the variable is a boolean.

Upvotes: 4

Related Questions