Say you have class A, which classes A1, A2, and A3 inherit from. All four classes implement a class method:
+ (NSString *)idString;
Now say you have an object
A *object1; //object may be of type A1, A2, or A3
Is there any way to call the appropriate idString method? I thought maybe:
[[object1 class] idString]
But that won’t compile, says no know class method for selector idString. I also thought of treating idString like a private method and calling
[object1 idString]
But that says no interface declares the selector idString
Getting the class object of the instance you have,
[object class]and sending a message to that, is exactly how this is supposed to work. Classes are objects too in ObjC, and you can send the same message to different classes in exactly the same way you can sendintValueto either anNSStringor anNSNumberinstance (or,initto (nearly) any instance!). This is fundamental to ObjC – the method is looked up based on the message at runtime. If this line:isn’t compiling, then you have an error elsewhere. That’s completely legal and the way you do what you’re describing.