• 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: why is this Swift initializer legal
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why is this Swift initializer legal


  • Subject: Re: why is this Swift initializer legal
  • From: Roland King <email@hidden>
  • Date: Mon, 08 Jun 2015 08:10:47 +0800

> On 8 Jun 2015, at 06:14, Greg Parker <email@hidden> wrote:
>
>
>> 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.
>

Seems not to matter if it’s NSObject or not, appears to be subclassing any class with one and only one no-arg initializer gets one implicitly synthesized in for you. This is Swift we’re talking about in this case, not obj C by the way. eg this compiles too even though Y’s init doesn’t have a super.init() call in it per the documented Swift rules.

import Cocoa

class X
{
	let i : Int

	init()
	{
		i = 123
	}
}

class Y : X
{
	let j : Int

	init( jj : Int )
	{
		j = jj
	}
}



_______________________________________________

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


References: 
 >why is this Swift initializer legal (From: Roland King <email@hidden>)
 >Re: why is this Swift initializer legal (From: Greg Parker <email@hidden>)

  • Prev by Date: Re: why is this Swift initializer legal
  • Next by Date: SKPaymentTransaction, and Receipt on OS X
  • Previous by thread: Re: why is this Swift initializer legal
  • Next by thread: Forwarding messages to another class
  • Index(es):
    • Date
    • Thread