从NSInvocation获取参数值

Chr*_*ett 13 cocoa objective-c

有人可以解释一下如何将值传递给使用时被拦截的不存在的方法:

+ (void)forwardInvocation:(NSInvocation *)anInvocation;

+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
Run Code Online (Sandbox Code Playgroud)

给出如下消息:

[SomeClass doSomething:@"theThing" withSomething:@"aParam"];
Run Code Online (Sandbox Code Playgroud)

我可以毫无问题地获得方法签名,但我对如何获取传入的值非常困惑.

当我应该使用这些方法或者只是遗漏某些东西时,我完全偏离了基础吗?

Dav*_*ong 33

-[NSInvocation getArgument:atIndex:]

所以在你的情况下,你会像以下一样使用它:

__unsafe_unretained NSString * firstArgument = nil;
__unsafe_unretained NSString * secondArgument = nil;
[theInvocation getArgument:&firstArgument atIndex:2];
[theInvocation getArgument:&secondArgument atIndex:3];
NSLog(@"Arguments: %@ and %@", firstArgument, secondArgument);
Run Code Online (Sandbox Code Playgroud)

请记住,self并且_cmd是参数0和1.

  • 在ARC下,firstArgument和secondArgument需要__unsafe_unretained吗? (5认同)