tea*_*bot 9 iphone reflection runtime objective-c
如果我拥有的是它的字符串形式的签名,我如何在Objective-C类的运行时调用方法:
NSString* typeName = @"Widgets";
NSString* methodName = [NSString stringWithFormat:@"add%@Object:", typeName];
Run Code Online (Sandbox Code Playgroud)
请注意,方法名称可以在运行时更改,但参数的数量保持不变 - 在此实例中为1.
Tom*_*rys 26
您可以使用以下内容:
SEL selector = NSSelectorFromString(methodName);
[myObject performSelector:selector];
Run Code Online (Sandbox Code Playgroud)
还有performSelector:withObject:,和performSelector:withObject:withObject:方法,如果你需要传递参数.