iOS Development

Obj-C Convenience Categories

There are some things in in Objective-C that just bug me. They’re generally pretty small, but I find my self correcting in a lot of different projects. For instance, working with JSON frequently means you probably use NSJSONSerialization, and you may note that if a JSON key returns null, it will be put into the resulting object as [NSNull null]. Try sending [NSNull null] a message, and you get an exception… so we’re back to checking for null, which is what nil is supposed to save us from.

To get around this, I’ve created a Cocoapod (check it out on GitHub) which currently contains a few categories I find myself using over and over:

  • NSDictionary+ValueCheck – which has a method that checks if a key exists, and if it is not NSNull
  • NSDate+RelativeTime – which has self explanatory methods like isBefore and isAfter, as well as a method that allows you to easily add days, months, hours, whatever to an NSDate
  • NSArray+Reverse – which provides a method returning a reversed version of the array

All these categories are thoroughly tested (look for yourself!), and generally serve to make my code more readable and elegant. Lightweight, and easy to add to your project: just add

pod "SBCategories", :git => ''

to your Podfile and you’re good to go!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s