我需要在 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);
关于objective-c - 在 Cocoa/Objective-C 中获取方法签名的类型编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1538069/