加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Cocoa / Objective-C中获取方法签名的类型编码?

发布时间:2020-12-16 03:38:52 所属栏目:百科 来源:网络整理
导读:我需要在 Cocoa中使用“signatureWithObjCTypes:”构造一个任意的NSMethodSignature,而没有一个我可以通过“methodSignatureForSelector:”请求签名的对象. 为此,我需要方法编码,例如,是 c12@0:4@8 对于 (BOOL) isEqual: (id) object 我尝试使用@encode(…
我需要在 Cocoa中使用“signatureWithObjCTypes:”构造一个任意的NSMethodSignature,而没有一个我可以通过“methodSignatureForSelector:”请求签名的对象.

为此,我需要方法编码,例如,是

c12@0:4@8

对于

(BOOL) isEqual: (id) object

我尝试使用@encode(…)来获取类型编码,但这似乎不适用于函数(它导致未知类型’?’).我不想手动编码函数类型,因为它不能在不同的运行时间移植.

还没有声明的方法来获取编码.

还有另一种获取编码的方法吗?

问候,

约亨

解决方法

怎么样的:
#import <objc/runtime.h>
//inside the method implementation:
Method thisMethod = class_getClassMethod([self class],_cmd);
const char * encoding = method_getTypeEncoding(thisMethod);

或者对于任意方法:

#import <objc/runtime.h>
//inside the method implementation:
Method thisMethod = class_getClassMethod([self class],@selector(isEqual:));
const char * encoding = method_getTypeEncoding(thisMethod);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读