Categories are an Objective-C syntax for adding features to a class without subclassing it and without even having access to its code (its .m file).

This comes in handy when you want to keep using a specific class (for example NSString) but would like to add an extra feature.

We have seen categories before, when using the AFNetworking framework. UIImageView+AFNetworking is a category on UIImage that allows setting an image using a URL. It does this by adding methods to UIImage such as setImageWithURL: and setImageWithURL:placeholderImage:.

Categories have their own .h and .m files. These are usually named +. These can be easily created through the New File menu in Xcode.

Header Source

Categories can have their own methods and properties, but no instance variables. This means that properties must be implemented and cannot be synthesized.

Header Source

To use a category, we just need to import the header into the class where we need the additional functionality. This will make the extra methods available to use.

Comments are closed.