• 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: Is "-init" really needed?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is "-init" really needed?


  • Subject: Re: Is "-init" really needed?
  • From: Quincey Morris <email@hidden>
  • Date: Tue, 8 Aug 2017 10:35:18 -0700

I don’t have a problem with using “new” vs. alloc/init, but there are a couple
of technical issues hiding behind this, though in current practice they make
little difference.

1. NSObject has “new” with a standard meaning. That means you can use “new” on
any subclass where it’s OK to use the simple “init” without parameters.
However, for classes that need parameters, or have multiple inits, there’s no
standard “newWithX:…” class method unless you provide it yourself.

You can’t really provide this for Cocoa classes (you could try adding it via a
category, but this seems like a bad idea), which means your code uses a mixture
of strategies to create instances, even of the same class, depending on the
parameters needed. If that doesn’t bother you, fine. This is purely a matter of
preference.

2. In modern Obj-C, a class method beginning with “new…” is specially
meaningful to the compiler under ARC. By default, it has +1 ownership semantics
for the returned reference. Using Jens’ earlier example, that means that the
following are subtly different:

>       [NSArray array]         // +0 semantics
>       [NSArray new]           // +1 semantics

For custom “new…” methods, the declaration can override the default semantics
with a compile-time attribute, which means you cannot in general be certain of
the semantics without looking at the declaration.

Of course, if the *calling* code is also ARC, then it doesn’t matter, because
ARC keeps track of the semantics for you.

So, as I said, little practical difference.
_______________________________________________

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

  • Follow-Ups:
    • Re: Is "-init" really needed?
      • From: Alex Zavatone <email@hidden>
References: 
 >Is "-init" really needed? (From: David Hoerl <email@hidden>)
 >Re: Is "-init" really needed? (From: Jens Alfke <email@hidden>)
 >Re: Is "-init" really needed? (From: じょいすじょん <email@hidden>)
 >Re: Is "-init" really needed? (From: Carl Hoefs <email@hidden>)
 >Re: Is "-init" really needed? (From: Uli Kusterer <email@hidden>)
 >Re: Is "-init" really needed? (From: Alex Zavatone <email@hidden>)
 >Re: Is "-init" really needed? (From: Steve Mills <email@hidden>)

  • Prev by Date: Re: Is "-init" really needed?
  • Next by Date: Re: Is "-init" really needed?
  • Previous by thread: Re: Is "-init" really needed?
  • Next by thread: Re: Is "-init" really needed?
  • Index(es):
    • Date
    • Thread