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

前端 未结 2 1987
天涯浪人
天涯浪人 2021-01-04 19:07

Assuming object is a kind of NSObject, the following if statements are equivalent, but which style should I use?

if (object) {
             


        
相关标签:
2条回答
  • 2021-01-04 19:42

    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 讨论(0)
  • 2021-01-04 19:45

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

    which style should I use?

    Whichever one you want.

    0 讨论(0)
提交回复
热议问题