JuJoDi JuJoDi - 1 year ago 89
Swift Question

Error in Swift class: Property not initialized at super.init call

I have two classes,


class Shape {
var numberOfSides = 0
var name: String
init(name:String) {
self.name = name
func simpleDescription() -> String {
return "A shape with \(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 the implementation above I get the error:

property 'self.sideLength' not initialized at super.init call

Why do I have to set
before calling

Answer Source

Quote from The Swift Programming Language, which answers your question:

“Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error:”

Safety check 1 “A designated initializer must ensure that all of the “properties introduced by its class are initialized before it delegates up to a superclass initializer.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download