In Objective-C I know you can forward selectors from one instance to another:
- (id)forwardingTargetForSelector:(SEL)aSelector;
How can I also forward class methods to another class? resolveClassMethod:
didn't seem appropriate, because I don't want to dynamically add methods to my class, just forward the calls.
Edit: So far the best I have is to create an instance that forwards its selector to a class object, like this:
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return NSClassFromString(@"TheClass");
}
Its just a little uglier, because the proxy object has to accept instance methods, so the proxy has to declare instance methods for the class methods it is proxying.
In An Overview of Objective-C Object Oriented Programming we learned that instance methods are prefixed by the minus sign (-). We declare class methods using the plus sign (+) as follows in our BankAccount.h file: We now have our two class methods declared.
To save you going through the old article, here's the issue: let's say you have some existing C++ code, a library perhaps, and you want to use it in an Objective-C application. Typically, your C++ code will define some class you'd like to use. You could switch your whole project to Objective-C++ by renaming all the. m files to.
While creating a Objective-C method, you give a definition of what the function has to do. To use a method, you will have to call that function to perform the defined task. When a program calls a function, program control is transferred to the called method.
A method definition in Objective-C programming language consists of a method header and a method body. Here are all the parts of a method: Return Type: A method may return a value. The return_type is the data type of the value the function returns.
In Objective-C a class object is just an object like any other and support forwarding in the same way. What you are looking for is:
+ (id)forwardingTargetForSelector:(SEL)aSelector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With