• 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: self vs. [self class] in class methods: is there a difference?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: self vs. [self class] in class methods: is there a difference?


  • Subject: Re: self vs. [self class] in class methods: is there a difference?
  • From: Matt Neuburg <email@hidden>
  • Date: Sat, 18 May 2002 10:37:06 -0700

On 16 May 2002 13:48:27 -0000, "Rajiv Vijayakumar"
<email@hidden> said:

>I've reproduced the relevant quote from the book below; it is from page
>100, and the context is a discussion of how to write the convenience
>constructor +[Rectangle rectangleOfColor:]
>
>"...rather than sending the alloc message to self in a class method, it's
>often better to send alloc to [self class]. This way, if the class is
>subclassed, and the rectangleOfColor: message is received by a subclass,
>the instance returned will be the same type as the subclass."

I agree; the writer of the doc is confused. "self" means "the original
recipient of the message". [self alloc] all by itself will return an
instance of the same type as the subclass.

To prove this to yourself, create Rectangle and Square and give Rectangle
this class method:

NSLog (NSStringFromClass([[self alloc] class]));
NSLog (NSStringFromClass([[[self class] alloc] class]));

Now call:

[Rectangle sayHowdy];
[Square sayHowdy];

The result:

Rectangle
Rectangle
Square
Square

This proves that there is no distinction between what [self alloc] returns
and what [[self class] alloc] returns, and that what is returned is always
the right thing.

m.
--

matt neuburg, phd = email@hidden, http://www.tidbits.com/matt
pantes gar anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Dock Magnification Effect
  • Next by Date: Re: NSApplication:showHelp: "set the application's help file using Project Builder" -- HOW?
  • Previous by thread: Re: self vs. [self class] in class methods: is there a difference?
  • Next by thread: Finding myDocument from AppController
  • Index(es):
    • Date
    • Thread