I used to see conditions like
if (something == 0){do it}
, but
now I've met this sentence in iOS_Book
if (nil == s)
Is this the same as if (s == nil)
and if not, what is the difference?
I used to see conditions like
if (something == 0){do it}
, but
now I've met this sentence in iOS_Book
if (nil == s)
Is this the same as if (s == nil)
and if not, what is the difference?
They are the same for testing, but putting the nil to the left will save you from the accidental assignment bug if you inadvertently use =
instead of ==
.
The two forms do exactly the same thing, although the nil == s
has one very nice advantage: You'll get a compile error if you make a typo and write =
instead of ==
, as you can't assign to a constant. This is often referred to as 'Yoda Notation'.