How can I forward class methods in Objective-C?

不问归期 提交于 2019-12-01 04:42:31

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22622747/how-can-i-forward-class-methods-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!