Search code examples
objective-cobjective-c-category

Is it safe to override a Category-defined method in Objective-C?


I have a class structure of the type UIViewControllerSubclass : UIViewController, where the only function of UIViewControllerSubclass is to #import UIViewController+Category.h. The reason I added methods in a category is so that I can also make UITableViewControllerSubclass : UITableViewController, which will #import UIViewController+Category.h as well. As we all know, don't repeat yourself.

Now assume that UIViewController+Category.h has the structure:

@interface UIViewController(Category)
- (void) method1;
- (void) method2;
@end

How safe is it to create UIViewControllerSubclassSubclass : UIViewControllerSubclass, which will override method1? I assume this will work because of Objective-C's message passing, but for some reason my intuition is telling me that I'm doing it wrong.


Solution

  • Everything should work fine since the category is applied to UIViewController, so all instances of UIViewController, including subclasses, will have access to the methods. There's nothing unsafe about it; that's how categories are intended to be applied.