It seems to me that the outcomes you’ve described are what would be expected, 
except in a couple of details.

On Feb 13, 2015, at 12:17 , Mike Ellard 
<[email protected]> wrote:
>
> What I would expect:
>
> A) Swift would not expose alloc or `new` at all.  There shouldn’t be a way to 
> create objects without going through an initializer.

I don’t know why you included ‘new’ in here. It’s valid to use it as a factory 
method in a subclass of NSObject, and I assume the behavior you’re seeing comes 
from Swift treating it as a factory method because of its name. As you showed, 
it doesn’t *not* go through an initializer. I don’t see how Swift could 
disallow it.

> B) If a class does override alloc (either in Swift in or a mixed Objective-C 
> / Swift project), then initializing an object would call the overridden alloc 
> method.

This is the one that’s a bit puzzling. There are frameworks classes (such as 
NSArray IIRC) that return a placeholder object from ‘alloc’ and do their actual 
memory allocation in ‘init’, and creating such objects in Swift would break (or 
would they?) if Swift used an object creation methodology that didn’t do 
alloc/init in the Obj-C way.

It’s possible that Swift alloc/inits Obj-C-style objects in the Obj-C-normal 
way, but that alloc and init overrides don’t produce the same runtime class 
structure as the corresponding Obj-C overrides.

I think your test needs to be a bit more intricate. What happens if you 
subclass NSObject in Obj-C with an alloc override, then sub-subclass that in 
Swift, also with an alloc override. Which, if any, allocs are invoked in that 
case?

Similarly, what happens if you subclass NSObject in Swift with an alloc 
override, then instantiate that class in Obj-C using the standard alloc/init 
sequence? (You could sub-subclass in Obj-C for parallelism, if you wanted, but 
I suspect the simpler test will suffice.)

> C) That in Swift, the NSObject alloc method would be “final” so that it 
> couldn’t be overridden.

As before, I’m not sure why you would want this. If Swift and Obj-C are to be 
interoperable, doing the override in Swift ought to be possible, even if for 
other reasons it only executed when an instance was created from the Obj-C side.



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to