properties - Error in Swift class: Property not initialized at super.init call -
i have 2 classes, shape , square
class shape { var numberofsides = 0 var name: string init(name:string) { self.name = name } func simpledescription() -> string { return "a shape \(numberofsides) sides." } } class square: shape { var sidelength: double init(sidelength:double, name:string) { super.init(name:name) // error here self.sidelength = sidelength numberofsides = 4 } func area () -> double { return sidelength * sidelength } } with implementation above error:
property 'self.sidelength' not initialized @ super.init call super.init(name:name) why have set self.sidelength before calling super.init?
quote swift programming language, answers question:
“swift’s compiler performs 4 helpful safety-checks make sure two-phase initialization completed without error:”
safety check 1 “a designated initializer must ensure of “properties introduced class initialized before delegates superclass initializer.”
excerpt from: apple inc. “the swift programming language.” ibooks. https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
Comments
Post a Comment