[Objective-C] Proper way of calling an overrided base interface method while using a category

Hi,

For some of my use cases, I'm extending an interface using a category. I am overriding some of the methods in the base Interface, but want to call the base interface method after I'm done with the workflow inside the category method. Essentially, doing something similar to a "super" call.

While I was looking into how to achieve this, have found something called Method Swizzling( https://nshipster.com/method-swizzling/ , https://newrelic.com/blog/best-practices/right-way-to-swizzle), but this looks too 'hacky'.

Is there a better way to achieve this?

Replies

Your question doesn’t make sense. Extending a class using a category is not the same as creating a subclass, and the whole concept of overriding with inheritance and calling super only makes sense when working with a subclass.

If you add a method to a class via a category and the class already implements that method, it’s undefined which method will get called [1].

The standard solution here is to create a subclass. Why can’t you do that?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Well, there are actually rules here but those only help in specific cases, not in the general.

  • Can you please elaborate a bit more about those rules of calling methods when a category implements the same method that is defined in the class. Since, as I remeber, Apple's official documentation indeed says that it's undefined which method will be called. Thank you for your invaluable help for the community!

Add a Comment

Hi,

Wanted to share a bit more context as it seems my question didn't come out in the right way. I'm using an library for building my applications(Qt). The library itself has defined an ApplicationDelegate and has member functions inside it to handle various lifecycle events. I want to override the Library's methods to execute my own code in the lifecycle events, to setup my application and then hand it back to the library's method to continue execution.

I want to override the Library's methods to execute my own code in the lifecycle events

The correct approach here is to subclass the library’s app delegate class. You should ask the library’s vendor how to do that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can you please elaborate a bit more about those rules of calling methods when a category implements the same method that is defined in the class.

I don’t know what the rules are off the top of my head. This is the sort of thing I’m interested and so, in some other situation, I might spend time researching it. However, this specific case, where a method is implemented in both a class and a category on that class, is so clearly a bad idea that it’s not worth investing the time. Sorry.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"