ObjectiveC: if (obj) {…} AND if (obj != nil) {…}, which is better?

后端 未结 2 2032
抹茶落季
抹茶落季 2020-12-11 06:43

I\'ve seen a lot of ObjC code which do:

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

but as I understood it, the value inside () i

2条回答
  •  甜味超标
    2020-12-11 07:28

    0 indicates FALSE 1 indicates TRUE

    Close. In C (and Objective-C), a 0 evaluates to false, and a non-zero evaluates to true. So a nil (or NULL) pointer is "false", but any non-nil pointer is "true".

    Your examples are essentially equivalent; neither is "better" than the other (unless you or your codebase has a style preference).

提交回复
热议问题