2

I know you should not override methods in a category that are defined in the class the category is for. But what about overriding inherited methods. Is that OK?

Specifically, UITableViewController inherits methods like viewWillAppear:, viewWillDisappear, viewDidLoad: and so on, from UIViewController.

So, let's say in a category on UITableViewController, we override those methods inherited from UIViewController.

Is it OK?

As far as I can tell, this will only break if Apple in some future version of UIKit decides to override these methods in UITableViewController.

Are there other reasons not to do this?

EDIT:

So the part about overriding methods in categories in the documentation passed under my radar, so thanks for the answer.

I'll have to solve this with subclassing and possibly extensions.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Tom Erik Støwer
  • 1,399
  • 9
  • 19

1 Answers1

1

Overriding methods in a category is discouraged. See Overriding methods using categories in Objective-C

In my opinion it doesn't matter if this means overriding an inherited or class-defined method. Why should it make a difference? Why not subclass?

Community
  • 1
  • 1
Mario
  • 4,530
  • 1
  • 21
  • 32
  • The main motivation for using category is that I want to add multiple narrowly defined behaviors to my class. Since multiple inheritance is not an option, I don't see how I can achieve this without categories. – Tom Erik Støwer Mar 12 '14 at 21:27