用于从. NSSelectorFromString
_ 然后您可以使用其中一种方法执行它。SEL
NSString
performSelector
动态设置属性:
SEL setter = NSSelectorFromString(@"setProperty:");
[myObject performSelector:setter withObject:newValue];
动态获取属性:
SEL getter = NSSelectorFromString(@"property");
id myProperty = [myObject performSelector:getter];
对于更复杂的方法,您可以使用NSInvocation
and NSMethodSignature
:
SEL action = NSSelectorFromString(@"someMethod:withArguments:");
NSMethodSignature *signature = [myObject methodSignatureForSelector:action];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setArgument:arg1 atIndex:2]; // indices 0 and 1 are reserved.
[invocation setArgument:arg2 atIndex:3];
[invocation invokeWithTarget:myObject];
id returnedObject;
[invocation1 getReturnValue:&returnedObject];