woot586
woot586

Reputation: 3965

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” getImage];

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.

Upvotes: 0

Views: 293

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163238

Use NSClassFromString:

UIImage* image = [NSClassFromString(@“Jim”) getImage];

Upvotes: 2

Related Questions