我正在使用 objective-c 的 forwardInvocation:
功能,我需要知道该方法接收到的参数的类型。在我的示例中,我向它传递了一个 int
,但 getArgumentTypeAtIndex:
告诉我它是一个 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
作为类型吗?
最佳答案
问题是您在类上实际上没有stuff:
方法,因此methodSignatureForSelector:
将返回nil
- 它看起来您发现了这一点并因此实现了您自己的版本,但是在 super
调用中失败,因此最终返回 forwardInvocation:
的签名 - 这不是您想要的!
要解决这个问题,您需要将 methodSignatureForSelector:
指向具有选择器的类,或者使用协议(protocol) - 如果类实现了协议(protocol),那么它将返回任何方法的签名在该协议(protocol)中,即使这些方法实际上并未由该类实现。
这是您使用协议(protocol)的示例:
@protocol DoProtocol
@optional
+ (void) stuff:(int)x;
@end
@interface Do : NSObject<DoProtocol>
@end
@implementation Do
+ (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
@optional
避免了对未实现方法的任何编译器警告。 methodSignatureForSelector:
的默认实现(来自 NSObject
)将返回从协议(protocol)获得的有效签名,因此 forwardInvocation:
将被调用。
关于ios - 期望参数类型为整数,但获取的是 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909509/