2008 07 10Telling classes from instances
In ObjC, how do you know the kind of object you're handling ? Classes still seem a little mysterious to me as they're not defined as objects but behave like objects : you can call
objectis a class or an instance, and the runtime will automatically call class (
+) or instance (
-) methods. If comes the time where you need to tell them apart, a simple way is to check if an object matches its class :
// A class id object1 = NSClassFromString(@"NSApplication"); // An instance id object2 = [object1 sharedApplication]; // Telling instances from classes … // Evaluates to YES isClass = object1 == [object1 class]; // Evaluates to NO isClass = object2 == [object2 class];Or did I miss some obvious "already there" method ?