• 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
Init in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Init in Swift


  • Subject: Init in Swift
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Mon, 05 Sep 2016 12:50:51 +0700

I have a SuperClass with several Subclasses.
The SuperClass will never be instantiated. It just contains code common to all subclasses.

Here an example:
I really want “onlyKnownBySubclass” to be a constant (i.e. let instead of var). But cannot figure out  how to do this.

class SuperClass
{
	let knownBySuperclass: Int
	var onlyKnownBySubclass: Int	//	this is a constant, only set in SubClass.init

	init(some: Int)
	{
		knownBySuperclass = some * 2
		onlyKnownBySubclass = 0	//	this value will never be used
	}

	final func someFunction() -> Void
	{
		print("Should never be zero: \(onlyKnownBySubclass)")
	}
}

final class SubClass: SuperClass
{
	override init(some: Int)
	{
		super.init(some: some)

		//	depends on knownBySuperclass:
		onlyKnownBySubclass = knownBySuperclass + 5	//	constant - will never change after this
	}
}

let a = SubClass(some:11)
a.someFunction()	//	prints: “Should never be zero: 27”

Gerriet.


_______________________________________________

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


  • Follow-Ups:
    • Re: Init in Swift
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Get file argument
  • Next by Date: Re: Init in Swift
  • Previous by thread: Re: Get file argument
  • Next by thread: Re: Init in Swift
  • Index(es):
    • Date
    • Thread