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.

 

DeliciousTwitterFacebookRedditLinkedInEmail

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

©2012 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *