Last Updated: February 25, 2016
·
615
· huytoan

Simplify your nil checking code

In Objective-C, any message sent to a nil object will return nil, so this if statement:

if(myObject != nil && myObject.aProperty != nil) { ... }

can be reduced to:

if(myObject.aProperty != nil) { ... }

or even shorter:

if(myObject.aProperty) { ... }

Similarly, to check if an array is not empty, we just need to write

if(myArray.count) { ... }

instead of

if(myArray != nil && myArray.count > 0) { ... }