When to use categories and when to use subclassing?

philip abraham picture philip abraham · Nov 9, 2011 · Viewed 17.9k times · Source

Can anybody tell me when to use categories and when to use subclassing in Objective-C? Also please tell me the advantages and disadvantages of them.

Answer

coneybeare picture coneybeare · Nov 9, 2011

An objective-c category is useful if you want to alter the behavior of ALL instances of the class, with minimal code. Subclassing is more useful if you want to alter the behavior of only certain instances, and retain the original method for others.

Categories can be dangerous, especially if you cannot view the source of the original method, so you should generally use subclasses on third-party and private frameworks rather than a category.