How to dynamically add a method to a class in Objective-C?

I understand that the main advantage of Objective-C over C ++ is its ability to send messages to objects instead of calling its methods. Secondly, you are allowed to dynamically add a method to objects.

Suppose this is my object:

@interface MyClass : NSObject {} - sayHello; @end 

I know that my code below will work even if - sayGoodbye not defined, but can someone complete this code and demonstrate how Objective-C can add methods to objects at runtime?

 MyClass* o = [[MyClass alloc] init]; [o sayHello ]; [o sayGoodbye]; [o release ]; 
+4
source share
1 answer

Link to target c runtime is what you need here:

Objective-C Runtime Reference

in particular, consider the following method: class_addMethod

+2
source

Source: https://habr.com/ru/post/1386967/


All Articles