Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 848
Nick Duffell Avatar asked Oct 23 '12 09:10

Nick Duffell


Video Answer


1 Answers

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.

like image 80
zoul Avatar answered Sep 18 '22 05:09

zoul