Skip to content

Instantly share code, notes, and snippets.

@qnoid
Last active August 9, 2017 11:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnoid/1499fb8e0e344f706e00 to your computer and use it in GitHub Desktop.
Save qnoid/1499fb8e0e344f706e00 to your computer and use it in GitHub Desktop.
Swift ensures that all properties of a derived class are initialised before calling its base class. The compiler will generate a warning. Just move the statement below the assignment to remove the warning. Compared to Java, this is safe. See: http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors
import Cocoa
class Plane
{
init()
{
readyToTakeOff();
}
func readyToTakeOff(){
}
}
class HighSpeedPlane : Plane
{
var speed : Int
init(speed: Int) {
super.init() //compiler warning: "Property 'self.speed' not initialized at super.init call"
self.speed = speed
}
override func readyToTakeOff()
{
super.readyToTakeOff()
println("Travelling at the speed of \(self.speed)")
}
}
let plane = HighSpeedPlane(speed: 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment