cfi*_*her 36 cocoa cocoa-touch objective-c performselector
performSelector的返回值是什么:如果我传递一个返回基本类型(在对象上)的选择器,例如NSDateComponents上的'week'(将返回一个int)?
diz*_*izy 79
使用NSInvocation返回float的示例:
SEL selector = NSSelectorFromString(@"someSelector");
if ([someInstance respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[[someInstance class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:someInstance];
[invocation invoke];
float returnValue;
[invocation getReturnValue:&returnValue];
NSLog(@"Returned %f", returnValue);
}
Run Code Online (Sandbox Code Playgroud)
我认为你无法从performSelector获取返回值.你应该调查一下NSInvocation.
老问题新答案~
有一种更简洁的方法可以从中获取返回值 performSelector
NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(height) object:nil];
[invo start];
CGFloat f = 0;
[invo.result getValue:&f];
NSLog(@"operation: %@", @(f));
Run Code Online (Sandbox Code Playgroud)
其中
- (CGFloat)height {
return 42;
}
Run Code Online (Sandbox Code Playgroud)
输出
2017-03-28 16:22:22.378 redpacket[46656:7361082] operation: 42
| 归档时间: |
|
| 查看次数: |
18706 次 |
| 最近记录: |