Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I forward class methods in Objective-C?

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.

like image 827
MaxGabriel Avatar asked Mar 24 '14 23:03

MaxGabriel


People also ask

How to declare class methods in Objective-C object oriented programming?

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.

How can I use C++ code in Objective-C?

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.

How do you use methods in Objective-C?

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.

What are the parts of a method in Objective C?

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.


1 Answers

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
like image 119
CRD Avatar answered Sep 28 '22 17:09

CRD