ObjectiveC: if (obj) {...} And if (obj! = Nil) {...}, which is better?

I saw a lot of ObjC code that do:

obj = [[SomeObject alloc] init];
if (obj) {
/// ...
}

but, as I understand it, the value inside () is logical, and 0 indicates that FALSE 1 indicates TRUE (there is another case in another language, which is 0 and 1 is false), and if the pointer does not indicate anything, it set to NULL (nil), which #defined is 0, so I think whether it is better to do this:

if (obj != nil) {
/// ...
}

since it checks if obj is zero or not, no matter what the value is nil, so it does not rely on what nil(or NULL) is defined as 0?

+5
source share
2 answers

, , ;

:

(, )

if(obj) {

( ). , " " FALSE; , , - , 0 "" "". , - , 0 FALSE TRUE.

Objective-C nil 0 ( ). , . FALSE ( "NO" ).

, , ; , , nil typedef'd 0, , " ".

+5

0 , FALSE 1 TRUE

. C ( Objective-C) a 0 false, true. , nil ( NULL) "false", -nil "true".

; "", ( ).

+3

All Articles