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

Joc*_*hen 6 macos cocoa objective-c

我需要在Cocoa中使用"signatureWithObjCTypes:"构造一个任意的NSMethodSignature,而没有一个我可以通过"methodSignatureForSelector:"请求签名的对象.

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

c12@0:4@8
Run Code Online (Sandbox Code Playgroud)

对于

(BOOL) isEqual: (id) object
Run Code Online (Sandbox Code Playgroud)

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

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

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

问候,

约亨

Dav*_*ong 12

怎么样的:

#import <objc/runtime.h>
//inside the method implementation:
Method thisMethod = class_getClassMethod([self class], _cmd);
const char * encoding = method_getTypeEncoding(thisMethod);
Run Code Online (Sandbox Code Playgroud)

或者对于任意方法:

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


Geo*_*lly 0

它不是只能使用未知类型编码吗?

?| 未知类型(除其他外,此代码用于函数指针)

AFAIK 这应该不重要,因为这只是争论的大小。函数指针与标准参数(int)具有相同的大小。