Re: Another Gnarly Objective-C Question!
Re: Another Gnarly Objective-C Question!
- Subject: Re: Another Gnarly Objective-C Question!
- From: Jens Alfke <email@hidden>
- Date: Tue, 12 Mar 2013 11:38:45 -0700
On Mar 12, 2013, at 2:08 AM, Graham Cox <email@hidden> wrote:
> A class method is just an instance method of the class object. No magic at all. So all this confusion you've caused yourself about [super class] and so on is wholly unnecessary to correctly use class methods.
Agreed. This can be confusing to people coming from less-dynamic languages like C++ or Java.
* In Objective-C a class is an object. You can think of it as a singleton — so when “Foo” appears as the receiver in an expression (like “[Foo alloc]”) it means the singleton object that represents the class Foo.
* Class (“+”-prefixed) methods are instance methods of the class object.
* “super” works as it does for instances, so in the implementation of +[Subclass foo], calling “[super foo]” will invoke the superclass’s implementation of +foo, if it has one.*
* Calling +class on a class object is a no-op, because it’s already a class. The only reason this is useful is it allows you to say “[Foo class]” to get the class object of Foo, because for some syntactic reason just saying “Foo” doesn’t work.
—Jens
* The details of this are really weird and involve the existence of metaclasses, which have an even weirder relationship to each other, but fortunately you almost never have to think about this in Objective-C. It’s much more explicit in Smalltalk-80, where these concepts originate. Look up “metaclass” on Wikipedia if you want the gory details.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden