• 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: [Foo new] vs [[Foo alloc] init]:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Foo new] vs [[Foo alloc] init]:


  • Subject: Re: [Foo new] vs [[Foo alloc] init]:
  • From: Adam P Jenkins <email@hidden>
  • Date: Fri, 15 Feb 2008 18:05:04 -0500


On Feb 15, 2008, at 4:55 PM, Gregory Weston wrote:

Keith Duncan wrote:

As far as I understand, [Foo new] is exactly equivalent to [[Foo
alloc] init]

Correct.

Incorrect. Based on the documentation new (by default) does setup work analogous to alloc and then invokes init before returning the object. It does not actually use the alloc method, though, which means it's entirely possible for new to not be equivalent to an alloc/init pair.

The only ways it could be different are:

1. If +new does not in fact call +alloc, but only does something equivalent, then if a subclass overrode the +alloc method but didn't also override +new, then it would behave differently. Do any classes actually override +alloc? If so then that might be a good reason not to use +new.

2. If a class overrode +new to do something different. In that case though the class's author probably did that on purpose, so you'd probably still be correct to call +new.



Chaining alloc and init is just the in vogue convention as far as I
know. I've been told that -new used to be a popular way of doing it
back in the days of NeXTStep.

It's more than just the 'in vogue convention' though. The point was to recognize that allocation and initialization are distinct actions and there's benefit in having them be separable. With alloc/init you can change one of the phases without concern for the other.

I never meant to imply that there isn't a benefit to having them be separable. It's very common to want to use a different initializer than -init, or to want to allocate an object in some other way than what +alloc does. I'm only talking about the common case where you ARE just chaining +alloc and -init together, and wondering why people don't just use +new instead.


_______________________________________________

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: 
 >Re: [Foo new] vs [[Foo alloc] init]: (From: Gregory Weston <email@hidden>)

  • Prev by Date: Re: folder content as model for NSTableView
  • Next by Date: Re: logging object creations and destructions
  • Previous by thread: Re: [Foo new] vs [[Foo alloc] init]:
  • Next by thread: Re: [Foo new] vs [[Foo alloc] init]:
  • Index(es):
    • Date
    • Thread