0

In objective C, classes are objects, but what object owns the class objects?

I am trying to get as detailed an understanding of iOS programming as I can, and this question popped into my head, so I decided I should know the answer.

6
  • What do you mean by "owns"?
    – user40980
    Jan 30, 2014 at 19:47
  • 1
    It's called a metaclass. This is interesting because at the top of the chain, one class is an instance of itself.
    – amon
    Jan 30, 2014 at 19:48
  • Does each instance of a class inherit pointers to all the other class objects? Jan 30, 2014 at 19:59
  • 1
    I just don't understand how I can be writing the .m file of a new subclass of NSObject, and I can call [NSString stringWithString:] as if the NSString class is an instance variable of my class. Jan 30, 2014 at 20:03
  • Oh, so your question is not about metaclasses, but about how the class names are visible in your scope? Aren't they just definitions you import through header files?
    – amon
    Jan 30, 2014 at 20:16

1 Answer 1

3

In objective C, classes are objects, but what object owns the class objects?

For an object to stay around it's not necessary to be owned by someone. They are just never released.

The runtime of course has pointers to all classes, so in a way you could argue class objects are owned by the runtime.

Where does the inheritance chain end?

The inheritance chain (or, rather, superclass chain) starts at the class of an instance and leads up the superclass and superclass's superclass...

It ends at the so called root class (there are several, like NSObject, NSProxy, and you can make your own). Objective-C has another complication: the class's class, called the "meta class". There's a meta class chain as well. See Greg Parker's diagram.

Does each instance of a class inherit pointers to all the other class objects?

No. Each instance has exactly one pointer to its class, the isa pointer.

Why am I able to send class objects messages? Are class objects global variables?

They are symbol references put into the executable by the compiler, a concept very similar to global variables. An @implementation defines the symbol.

You can send messages to classes because that's how the Objective-C syntax is defined.

Not the answer you're looking for? Browse other questions tagged or ask your own question.