Copyright 2004 by M. Uli Kusterer Tue, 30 Dec 1969 07:58:58 GMT Comments on article blog-classes-are-objects-too at Zathras.de http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm blog-classes-are-objects-too Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 10 by Matthieu Cormier http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment10 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment10 Matthieu Cormier writes:
Under,

"...what gets executed is equivalent to:"

MySubClass* newObject = [[[MySubClass alloc] init] autorelease];

could probably be...

MyBaseClass* newObject = [[[MySubClass alloc] init] autorelease];

for clarity.
Comment 9 by charles Parnot http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment9 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment9 charles Parnot writes:
Karsten: the problem is that all class objects are instances of the same class 'MetaClass'. Thus, all class objects for all the classes in your project would need to have the same instance variables.

Of course, at this stage, you could argue that class objects could be instances of MetaClass subclasses, and thus define their own instance variables (note that by nature, they would be singleton instances). But that would mean rewriting some of the basics of the runtime and the compiler.

That would solve the behind-the-scenes stuff. We would still need some Objective C syntax to accomodate class variables. Again, more work on the compiler.

Until that happens, we can just use plain C and static variables!
Comment 8 by xebecs http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment8 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment8 Karsten: Thanks for a bit of extra information. Now I realize what I was doing. I still think the compiler may not be giving a warning when it should.

@interface MyWebViewController: UIViewController <UIWebViewDelegate>
{ blah }
+ (UIView*) makeWebView
{
UIWebView *webView = // alloc, init
webView.delegate = self;
return [webView autorelease];
}

While an object of this type is a UIWebViewDelegate, the class is NOT! ...is it?

If not, why doesn't the compiler warn about the MyWebViewController class not being a UIWebViewDelegate?

[This is the actual problem I had -- I was misremembering from 3 projects back.]
Comment 7 by Jean-Daniel Dupas http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment7 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment7 "class methods in Objective C are exactly the same as instance methods"

My own version of this sentence to confuse people is "class methods are instance methods of class objects" ;-)

One feature of this design that is often overlooked is the ability to use a Class in all methods that take an object and a selector as arguments (notification observer, delegate, etc…).

Just pass the Class object and the selector of a class method, and it works.
Comment 6 by Uli Kusterer http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment6 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment6 Uli Kusterer writes:
Adam,

I forgot an "otherwise it fails there", thanks for the correction. I think I poured a steaming hot cup of typoes over this article :-(
Comment 5 by Karsten http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment5 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment5 Karsten writes:
Michelle: if you call [self thankUli: @"profusely"] from a class method then this is just alright...if you call it from an instance method you'll get a warning that this method does not exist. Then you could do [[self class] thankUli...].

Uli: one thing i still miss in Objective C: the classes are objects, but there's no way in the syntax to define instance variables for classes, while the runtime already supports them, i think.
Comment 4 by Adam http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment4 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment4 Adam writes:
Good article there. Sorry, I just started reading your blog again after a long time.

One tiny thing though:
-forwardInvoction: only gets called if the object and it's superclassses do not respond to the selector, i.e., you are only given a chance to forward when it would otherwise give up and raise an NSInvalidArgument exception (in the NSObject case). I imagine this is for performance reasons; it would be pointlessly innefficent to have to construct an NSInvoction object, send -methodSignitureForSelector: and -forwardInvoction: every time you call a method.
Comment 3 by Uli Kusterer http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment3 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment3 Uli Kusterer writes:
Ahruman, you're right. Brainfart. Fixed.
Comment 2 by Ahruman http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment2 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment2 Ahruman writes:
‘The superclass of these objects is "Metaclass".’

No no, not at all. The _class_ of a class object is a metaclass. The superclass of a class object is the class of the superclass of the class. Classy, innit? ;-)
Comment 1 by Michelle http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment1 http://www.zathras.de/angelweb/blog-classes-are-objects-too.htm#comment1 I *think* I followed you through all of that. But there is one oddity of Objective-C I still don't get.

Let's say I have a class method:

+ (void) thankUli: (NString*) howMuch { }

Why do I get no compiler error when I mistakenly invoke it as below?

[self thankUli: @"profusely"];

I've had some very subtle bugs from this issue. I like to use class methods to improve encapsulation, but it's very easy to just type "self" without thinking.