Skip to content

Instantly share code, notes, and snippets.

@ColinEberhardt
Created December 3, 2014 06:46
Show Gist options
  • Select an option

  • Save ColinEberhardt/36fe15ddee565af556b2 to your computer and use it in GitHub Desktop.

Select an option

Save ColinEberhardt/36fe15ddee565af556b2 to your computer and use it in GitHub Desktop.
Swift class constants are mutable within the initialiser
// A follow-up to the blog post here:
// http://www.scottlogic.com/blog/2014/11/20/swift-initialisation.html
class Foo { }
class Bar: Foo {
// no initial value set here
let myConstant: Int
override init() {
// the constant needs an initial value - but
// you can set it more than once!
myConstant = 77
myConstant = 45
super.init()
// you can set it here also
myConstant = 55 // 55
println(myConstant)
}
func doSomething() {
// but of course you cannot set it here!
//myConstant = 66
}
}
let bar = Bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment