Re: why is this Swift initializer legal
Re: why is this Swift initializer legal
- Subject: Re: why is this Swift initializer legal
- From: Greg Parker <email@hidden>
- Date: Sun, 07 Jun 2015 15:14:10 -0700
> On Jun 6, 2015, at 2:43 AM, Roland King <email@hidden> wrote:
>
>
>
> public class RDKBLEService : NSObject
> {
> let peripheral : CBPeripheral
>
> public init( peripheral: CBPeripheral )
> {
> self.peripheral = peripheral
> }
> }
>
> It’s a designated initialiser, there’s a superclass (NSObject) but the initialiser doesn’t call a designated initialiser of the superclass. According to the rules I was just re-re-re-reading about Swift initialisation, it’s required to call a superclass designated initialiser from your derived class. I was looking to see if I could find an exception to the rule which this fell under but can’t.
There is an exception in the designated initializer rule: if your superclass is NSObject then you may omit the call to -[NSObject init]. The compiler knows that -[NSObject init] does nothing, so it allows this as a performance optimization.
NSObject Class Reference:
"The init method defined in the NSObject class does no initialization; it simply returns self."
There is lots of existing code that would break if we changed that, so we can't even if we wanted to.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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