about categories and overridden methods...

Hi,


some of my classes are gowing very big. I would like to use categories for code management. I would also like to override some methods like viewDidLoad inside the category and continue its implementation from there.


Now in inheritance we can call [super viewDidLoad] to see to it that the actualy implementation of viewDidLoad is always called before the trailing part of the method is executed in out subclass.


How can we ensure this in a category?

I'm pretty sure you're asking about a distinction that doesn't exist.


As far as the runtime mechanism for finding methods is concerned, there's no difference between a method defined on class Such-and-Such and a method defined in Such-and-Such(Some-Extension). As a result:

  • You can't use an extension to Such-and-Such to override a method on Such-and-Such.
  • If you define a method on Such-and-Such or on Such-and-Such(Some-Extension) which overrides a superclass method, the runtime behavior is the same.
  • A method can call [super soAndSo] whether it's defined in Such-and-Such or Such-and-Such(Some-Extension) and the same behavior will occur.

assuming both Such-and-Such and Such-and-Such(SomeExtension) are linked in the project.

I havent explored extensions yet, but categories can override methods.

If you declare


interface MyClass extends SomeSuperclass {

void someMethod {

}

}

and then in a class extension declare the same method

interface MyClass(SomeExtension) {

void someMethod {

}

}

the behavior is undefined. The runtime is going to break your code if you try that, either arbitrarily now or in the future. Sure, if SomeSuperclass defines someMethod and you want to override it, you can declare someMethod in either MyClass or an extension to MyClass to do that.


But, no, you can't override a method in a class in its extension. And, because of that, there's no syntax to refer to the "The method defined in the main class instead of the class extension."


You can set up something like that manually in code using swizzling. But the extension mechanics won't do that for you. Extensions aren't informal subclasses.

1. I am talking about categories and not extensions. 2. I am talking about overriding methods in the Class of the Category and not the Superclass of the Category's class.

You can't provide a duplicate implementation in a category.


However, if you are providing implementations in a subclass, and then breaking those implementations in to categories, then that is fine.


My personal confusion is that your question seems to indicate that you want to provide an implementation for the class, and then override that implementation in a category, and call the original implementation. This is what NotMyName was saying that you can't do.


So, if you are simply breaking your code into categries, you need do nothing. Calling [super viewidLoad:view] will work exactly as expected, whether being called from a category or a direct class implementation.

Runtime details aside, I think you could break your code up into more manageable chunks by doing something simple like:


- (void)viewDidLoad {

[self doSomeStuff];

[self doSomeOtherStuff];

[self doSomeMoreOtherStuff];

[super viewDidLoad];

}


and then implement those methods in categories, likely in other files if that makes the most sense.


But I do have to ask: why is your view doing so much work in viewDidLoad? What is it that has your code growing so large that you're looking at breaking it up? If you've got too much going on here I would be looking at whether you can do things more lazily (or not at all!), or at least do things asynchronously in the background. Any code you put into viewDidLoad is going to be blocking the user from using the view that just loaded.

Obvious comman sense has abandoned me! I just need to split my code in viewDidLoad into separete methods. Lets just say the fatigue on being in a project too long, thats just the first project which turned out to be very big and wasn't well planned out of inexperience, is taking over me. I need to take a break.

about categories and overridden methods...
 
 
Q