Is it:
For memory efficiency from not having to store all the program’s methods in RAM all the time? If so, is this really that common a problem? I feel like the overhead of having to load a new method would cancel out the memory savings for normal-sized programs, although I can see how it would help for something very large.
For increased flexibility? If so, could you give have an example of that? I’m finding it difficult to think of one.
I have been trying to Google out the answer to this question and only seem to find resources on how to use categories rather than why. If any of you could point me in the right direction that would be awesome.
The main reason for categories is to allow you to add methods to a class for which you don’t have the source code, or for which you don’t want to modify the source code.
Example 1.
I wanted a method to create an animated
UIImageby loading an animated GIF. Logically this should be aUIImageclass method, but I don’t have the source code for the UIKit framework (which containsUIImage). So I wrote a category forUIImagethat adds a class method namedanimatedImageWithAnimatedGIFData:. You can find it in myuiimage-from-animated-gifrepository.Did I have to add this method to
UIImage? No. I could have made it a regular C function, or I could have made a utility class (perhaps namedAnimatedGIFLoader) to hold the method. But from a design standpoint, the method logically belongs onUIImage.Example 2.
Apple wanted to make it easy to draw a string into a graphics context. In a program with a GUI, it would be reasonable for
NSStringto have a draw method. Apple has the source code to the Foundation framework (which containsNSString), so they could add it. But the Foundation framework is designed to be used in all sorts of programs, including programs that don’t have any user interface. So the classes in Foundation don’t know anything about UIKit or AppKit or Core Graphics or any other higher-level library that can draw graphics.Instead, UIKit has a category that adds the
drawAtPoint:withFont:method toNSString. AppKit has a category that adds thedrawAtPoint:withAttributes:method toNSString.AppKit and UIKit have a number of other categories that add methods to Foundation classes. For example, UIKit has categories on
NSObject,NSIndexPath,NSCoder, and more.Another reason to use a category is to split up the implementation of a class into multiple
.mfiles. If you have a big class, you can move some of its selectors into a category and implement the category methods in a separate source file. The linker will automatically merge the category into the class when it creates the executable file, so there is no run-time penalty.