• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Another Gnarly Objective-C Question!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Another Gnarly Objective-C Question! (From: Dave <email@hidden>)
 >Re: Another Gnarly Objective-C Question! (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Objective-C Question
  • Next by Date: Re: Objective-C Question
  • Previous by thread: Re: Another Gnarly Objective-C Question!
  • Next by thread: Re: Another Gnarly Objective-C Question!
  • Index(es):
    • Date
    • Thread