Get Updates By E-mail 

Article Written By:
Johann Dowa

NSNull Category That Returns Nil Rather Than Throwing Exceptions When Using Null Objects

I’ve mentioned some excellent open source iOS projects from Nick Lockwood such as iRate for more app store ratings, and the AutoCoding library that automatically adds NSCoding/NSCopying support to your objects.

Update – Here’s an additional note added to the readme:

NullSafe will currently only respond to messages supported by NSValue, NSNumber, NSString, NSDictionary, NSArray, NSData and NSData

Here’s another very useful library from Nick called NullSafe that extends Objective-C with an NSNull category that simply returns nil rather than throwing an exception.

This is helpful for those places where you might usually use an if statement to check whether an object is null before passing it to a method.

As the readme states:

NullSafe is a simple category on NSNull that returns nil for any unrecognised messages instead of throwing an exception. This eliminates a common cause of crashes, where (for example) JSON data contains a null value instead of an array or string, and the network code in the app isn’t expecting it.

You can find NullSafe on Github here.

This should definitely help to knock out a lot of if statements from your code.


Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.