iPhone how to invoke a classes static function by string?
I have a static function defined in a class called "Character" as:
+ (UIImage) getImage;
I have a set of other classes that extend the “Character” class called “Bob” and “Jim”, each of these classes define a different file to get the image from. I need to be able to call get image based on the string e.g.
UIImage* image = [@“Jim” getIma开发者_运维百科ge];
However the above calls the getImage on the string class. I know it can be done like the following code snippet:
UIImage* image = [Jim getImage];
However I have a string not the object Jim. I suppose I could do a if statement that maps the string to a class but im wondering if there is a better way of dynamically calling a classes static function based on a string.
Use NSClassFromString
:
UIImage* image = [NSClassFromString(@“Jim”) getImage];
精彩评论