如何在Objective-C中转发类方法? [英] How can I forward class methods in Objective-C?

查看:109
本文介绍了如何在Objective-C中转发类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我知道您可以将选择器从一个实例转发到另一个:

In Objective-C I know you can forward selectors from one instance to another:

- (id)forwardingTargetForSelector:(SEL)aSelector;

如何将类方法转发到另一个类? resolveClassMethod:似乎不合适,因为我不想向类动态添加方法,只需转发调用即可.

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.

到目前为止,我最好的做法是创建一个实例,将其选择器转发到类对象,如下所示:

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.

推荐答案

在Objective-C中,类对象只是一个与其他对象一样的对象,并以相同的方式支持转发.您正在寻找的是:

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

这篇关于如何在Objective-C中转发类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆