Init in Swift
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