In obj-c, how can I call
[myClass myStri开发者_开发知识库ng];
where myString = @"myMethod";
Which should be the equivalent of [myClass myMethod];
Not sure if this kind of meta language manipulation is possible.
[myClass performSelector: NSSelectorFromString(myString)];
Docs:
- -performSelector: sends the selector specified to the receiver
- NSSelectorFromString() converts an NSStringinto a selector (typeSEL)
[myClass class] returns the metaclass, class methods are called on the metaclass. E.g.
[NSString someClassMethod];
NSString *instanceOfString = @"some string instance";
[[instanceOfString class] someClassMethod];
EDIT: I misread the question. Use NSSelectorFromString to get a SEL from an NSString. That's documented in Apple's Foundation Functions Reference where you'll also see NSClassFromString, NSStringFromClass, NSStringFromSelector, NSStringFromProtocol and NSProtocolFromString amongst others.
You can use NSSelectorFromString, something along the lines of the following should work:
 SEL selector = NSSelectorFromString(@"myMethod");
 [myClass performSelector:selector withObject:nil];
SEL mySelector = NSSelectorFromString(@"myMethod");
[myClass performSelector:mySelector];
Alternatively, in case you need more flexibility in argument and/or return types than performSelector: and friends will give you, have a look at the NSInvocation class.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论