Can category methods be overridden? IOS
Asked Answered
S

1

12

I am trying to plan how to add a couple methods to all instances of certain objects. I think adding a category to the parent object(UIViewController) would work for what I want to do, but can I override a method added this way? Most of the time the subclasses will use the default methods but I do know I will need to override the method at least once.

Also what other methods should I consider for what I am trying to do?

Example of what I am trying to do:

I have a set of Objects that act like pages of a journal. These pages are subclasses of UIViewControllers. I want to add methods for loading, saving, and similar. In a journal class(Subclass of NSObject) I have an array storing instances of each Journal page. I want it so I can select a page from the array and run on of the methods without having to know the specific object class as they all should have the methods.

Ship answered 16/7, 2012 at 17:54 Comment(0)
S
22

Short answer: yes, you can. For purposes of inheritance category methods on a superclass are treated like methods that are actually defined in the superclass, and can be overridden in the same way.

Sexagenary answered 16/7, 2012 at 18:44 Comment(3)
That makes sense, but I have a feeling the long answer might state something like "Apple does not aprove of this" or something else like that.Ship
Apple doesn't approve of using a category on a subclass to override a method in the superclass, but there is nothing wrong with using a method in a subclass to override a category on a superclass. In fact, whenever you subclass a framework class there's a good chance you're doing this without realizing it.Sexagenary
Thank you for the answer, but I found a beter way of doing what I want. Nothing wrong with your answer, just looked through my code and found a beter spot to do the additional code I needed without having to add methods.Ship

© 2022 - 2024 — McMap. All rights reserved.