开发者

Objective-C: if (object) Vs. if (object != nil)

开发者 https://www.devze.com 2023-03-13 03:20 出处:网络
Assuming object is a kind of 开发者_高级运维NSObject, the following if statements are equivalent, but which style should I use?

Assuming object is a kind of 开发者_高级运维NSObject, the following if statements are equivalent, but which style should I use?

if (object) {
    // ...
}

or

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


As you say, they're equivalent. Thus...

which style should I use?

Whichever one you want.


They are equivalent in the sense that they do the same thing. But I would argue that the second statement makes the code more readable. When a person reads the line, they will understand that it means "if object is not pointing to nothing."

Remember Knuth's dictum: a programming language is a way to deliver instructions to a machine in a human-readable form...

0

精彩评论

暂无评论...
验证码 换一张
取 消