Nick Duffell
Nick Duffell

Reputation: 340

How to store a Class variable, then call static methods of that class in Objective-C

So I want a class to be able to store a "Class" object, then later call the static methods of that class...

Basically I have multiple classes that inherit a single class, so they all have the same static methods (but return different things). I want to be able to store which subclass I am using so I know which one to call the static methods of...

I know I can get the class with

Class something = [VirginMobile class];

But I can't then do something like int i = [something staticMethodReturningInt];

Is there any way I can do this?

Cheers

Upvotes: 5

Views: 2108

Answers (1)

zoul
zoul

Reputation: 104065

I know you probably don’t want to hear that, but once you start being too clever with classes it’s a sure sign to use regular objects instead. I’m not sure if I understand your question correctly, but one way to solve issues with typing is to cast the receiver to id:

id something = [VirginMobile class];
int i = [something methodReturningInt];

This will compile fine, as long as the compiler can see the definition of methodReturningInt. And of course, at runtime VirginMobile has to respond to +methodReturningInt.

Upvotes: 6

Related Questions