如何从 NSInvocable 中获取 NSString 结果?
下面的代码按预期工作:
NSLog(@"%@", [NSString stringWithString:@"test"]; // Logs "test"
但是当我用 NSInitation
替换它时,我得到了完全不同的结果:
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"
我已经搜索了高低,但无法弄清楚这一点。有什么帮助吗?谢谢!
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 NSIncation 类参考:
当参数值是一个对象时,传递一个指向应从中复制该对象的变量(或内存)的指针:
由于 @"test" 实际上是构造 NSString 的实例,因此您应该使用
From the NSInvocation class reference:
When the argument value is an object, pass a pointer to the variable (or memory) from which the object should be copied:
Since @"test" is actually constructing an instance of NSString, you should use