11

If instance variables belong to an instance of a class, class variables would belong to an instance of a metaclass, I should think. But my experience with the Objective-C metaclass tells me that this is unlikely.

I'm wondering what class_getClassVariable does as opposed to class_getInstanceVariable, and why there is not a class_setClassVariable in the runtime.

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79

2 Answers2

13

I found a discussion about it here: http://lists.apple.com/archives/objc-language/2008/Feb/msg00021.html

Nope, it's not possible. There is no compiler support, and there is no runtime support; class_getClassVariable(cls, name) merely calls class_getInstanceVariable(cls->isa, name).

If it were possible, it likely would not give you a new variable on each subclass anyway. Objective-C's metaclasses are not quite first- class enough for that to work well.

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
Yuji
  • 34,103
  • 3
  • 70
  • 88
1

The class_getClassVariable() function doesn't make sense, because there's no such thing as a class variable in Objective C.

My guess is that class variables are supported by the runtime, but not by the language.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223