Objective-C: if (object) Vs. if (object != nil)
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...
精彩评论