Re: why is this Swift initializer legal
Re: why is this Swift initializer legal
- Subject: Re: why is this Swift initializer legal
- From: Roland King <email@hidden>
- Date: Sun, 07 Jun 2015 09:10:10 +0800
> On 7 Jun 2015, at 02:18, Marco S Hyman <email@hidden> wrote:
>
>> public class RDKBLEService : NSObject
>> {
>> let peripheral : CBPeripheral
>>
>> public init( peripheral: CBPeripheral )
>> {
>> self.peripheral = peripheral
>> }
>> }
>
> Swift doesn’t think init in NSObject is a designated initializer. Add
> “override” as you would with a designated initializer and you get an error
> stating “does not override a designated initializer from its superclass”.
>
> I do not know why.
>
> Marc
No that’s not correct I’m afraid. Swift does know init() in NSObject is a designated initializer. If I try to define a no-arg init() in my subclass then I get an error telling me I need to add the override keyword, which would be correct as I would then be trying to override NSObject’s designated initializer (hereafter abbreviated DI). The signature of my DI has an argument so it’s different so it’s not an override. It is however a DI and thus should by the very explicitly listed rules call a DI in the superclass
Oddly enough if I add any line after the self.peripheral = peripheral which uses self, anything, just a random thing like self.observationInfo (it was the first one under my cursor) then I get an error that I’m using self before calling a super.init call.
Quincey may well be right and in the case of a class which has one single, no-arg, DI that DI is automagically added in by the compiler at the end of the method. I rather hope that’s not true, Swift is already replete with little rules and oddities you have to know about, if the rule is you call a DI, you should have to call one, the compiler shouldn’t be ‘just letting you get away with it’ in certain cases.
_______________________________________________
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