Search code examples
iphoneobjective-c

object == nil or nil == object to check whether an object is nil?


While checking whether an object is nil, someone use 1:

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

someone use 2:

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

Any difference between 1 and 2? Which one is better?


Solution

  • The difference is mainly that if you mistakingly forget a = e.g like this

     (nil = myObject)
    

    you will get an error cause you can't assign a value to nil. So it is some kind of faile-safe.