Re: array = [NSArray new] or array = [NSArray array]?
Re: array = [NSArray new] or array = [NSArray array]?
- Subject: Re: array = [NSArray new] or array = [NSArray array]?
- From: Kyle Sluder <email@hidden>
- Date: Fri, 19 Aug 2016 20:25:27 -0500
On Fri, Aug 19, 2016, at 06:08 PM, Charles Srstka wrote:
> > On Aug 19, 2016, at 3:46 PM, Quincey Morris <email@hidden> wrote:
> >
> > On Aug 19, 2016, at 10:53 , Charles Srstka <email@hidden <mailto:email@hidden>> wrote:
> >>
> >> [NSArray array] is a synonym for [[[NSArray alloc] init] autorelease].
> >
> > (along with other similar comments in other responses)
> >
> > To be pedantic, it’s *implemented* as [[[NSArray alloc] init] autorelease], but the lack of an API contract to that effect means that “synonym” isn’t quite accurate.
> >
> > My point is that Apple might revisit the implementation of NSArray at any time (in these Swiftian days, stranger things have happened), and choose to write it with full ARC compatibility, which means there might not be any autorelease when invoked from ARC client code, since ARC can optimize the code when it handles both ends of the invocation.
>
> Swift typically just calls the initializer if it’s there, so the
> convenience initializers like +[NSArray array] are really only relevant
> to Objective-C code. For that reason, I doubt they’d change unless Apple
> did something crazy like rewrite the entire frameworks in Swift, in which
> case they’d probably go away.
There is an official reimplementation of Foundation/CoreFoundation in
Swift. The implementation of NSObject lacks a `static func new`:
<https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSObject.swift>
Likewise, Swift explicitly does not bridge `+new` from ObjC, so this is
a moot point in Swift code.
> So for the foreseeable future, the
> difference is that +array returns an autoreleased object, meaning that in
> ARC code, +new is the better choice.
I would not make that assumption. Who says +[NSArray array] constructs
anything at all? Try comparing the return values of two calls to
[NSArray array] sometime. ;-)
I happen think +new is more readable, but it’s really just a matter of
preference.
--Kyle Sluder
_______________________________________________
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