开发者

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜