Search code examples
swiftswift-playground

Swift class without initializer working?


Could you please tell me why that code (class) is working in playground? If I understand properly there should be init or something that can be used by "blank initializer"?

class Shape{
    var numberOfSides = 0
    func simpleDescription() -> String {
    return "A shape with \(numberOfSides) sides."
    }
}

var shapeA = Shape()
shapeA.simpleDescription()
shapeA.numberOfSides = 7
var shapeDescription = shapeA.simpleDescription()
shapeA.simpleDescription()

Thank you for your help


Solution

  • If all the stored properties are given default values, as here, the class does not need to have an explicit initializer.