[英]How can I get an NSString result from an NSInvocation?
The following code works as expected: 以下代码按预期工作:
NSLog(@"%@", [NSString stringWithString:@"test"]; // Logs "test"
But when I replace it with an NSInvocation
, I get a totally different result: 但是当我用
NSInvocation
替换它时,我得到一个完全不同的结果:
Class class = [NSString class];
SEL selector = @selector(stringWithString:);
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[class methodSignatureForSelector:selector]];
[invocation setTarget:class];
[invocation setSelector:selector];
[invocation setArgument:@"test" atIndex:2];
[invocation invoke];
id returnValue = nil;
[invocation getReturnValue:&returnValue];
NSLog(@"%@", returnValue); // Logs "NSCFString"
I've searched high and low, but cannot figure this out. 我搜索过高低,但无法弄清楚这一点。 Any help?
有帮助吗? Thanks!
谢谢!
From the NSInvocation class reference: 从NSInvocation类引用:
When the argument value is an object, pass a pointer to the variable (or memory) from which the object should be copied: 当参数值是对象时,将指针传递给应从中复制对象的变量(或内存):
NSArray *anArray;
[invocation setArgument:&anArray atIndex:3];
Since @"test" is actually constructing an instance of NSString, you should use 因为@“test”实际上构建了NSString的一个实例,所以你应该使用
NSString *testString = @"test";
[invocation setArgument:&testString atIndex:2];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.