Today, we discovered one of the coolest things about Objective-C EVER.

A way to make Objective-C NOT like Objective-C!

Objective-C Categories are a way to add functionality to any class, similar to subclassing, but the new functionality is applied to the existing class similar to prototyping in Javascript. The upshot of all this? All of the ridiculous contortions we have to go through to call the unfriendly APIs can have convenience methods applied to them with a Category!

One example: trim. In every good language, Strings can be trimmed to remove leading and trailing whitespace. In Objective-C you have to do it like this:

[myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

With the awesome application of a Category, we created our own trim method and now that code looks like:
[myString trim];


Making Lead into Gold!

Leave a Reply

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