6. Category

The Category pattern adds methods to existing classes and provides an alternative to subclassing in many situations. The Category pattern is supported directly by the Objective-C language and is used extensively in the implementation of other Cocoa design patterns. The added methods are inherited by all subclasses of the extended class and are indistinguishable at runtime from methods originally compiled into the class.

The Category pattern can also be used to replace the implementations of methods in existing classes, perhaps to fix bugs in framework classes without needing source code for those classes. However, there is no convenient way to call the original method from the code that replaces it. When replacing a method, you must ...

Get Cocoa Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.