我使用的Objective-C的forwardInvocation:
功能,我需要知道什麼說法類型收到的方法。在我的例子中,我通過了一個int
,但是getArgumentTypeAtIndex:
告訴我這是一個id
。這裏有一個簡單的例子:預計參數類型是整數,但得到的ID,而不是
@interface Do : NSObject
+ (void) stuff:(int)x;
@end
@implementation Do
+ (NSMethodSignature *) methodSignatureForSelector:(SEL)selector
{
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature)
signature = [self methodSignatureForSelector:@selector(forwardInvocation:)];
return signature;
}
+ (void)forwardInvocation:(NSInvocation *)i
{
const char* argType = [i.methodSignature getArgumentTypeAtIndex:2];
NSLog(@"%s == %s", argType, @encode(id)); // @ == @
NSLog(@"%s == %s", argType, @encode(int)); // @ == i
}
@end
以下是我把它叫做:
[Do stuff:123];
任何想法,爲什麼我沒有收到id
代替int
的類型?
這是有效的,但在我的情況下,我需要添加一個類別的協議,而不是原始的'@ interface'聲明。無論出於何種原因,這似乎並不奏效。 – bendytree
我最終直接從協議中提取簽名:'[NSMethodSignature signatureWithObjCTypes:protocol_getMethodDescription(@protocol(...),selector,0,0).types]' – bendytree