I'm trying to learn the concept of inheritance in Swift and have came up with the following code. I had no problem modifying variable "numberOfWheels" in the subclass, but when I tried to modify variable "description" in the subclass, Xcode display error:
cannot assign to property "description" is a get only property
Because I'm relatively new to Swift, I am unable to solve this problem? Could someone please provide me with a code example of how to solve this problem? Thank you very much for any help!
class Vehicle{
var numberOfWheels = 0
var description: String{
return "\(numberOfWheels) wheels"
}
}
class Bicycle: Vehicle {
override init(){
super.init()
numberOfWheels = 2
description = "\(numberOfWheels) wheels, more is good"
}
}
description
is a read-only computed property and therefore cannot be directly assigned to. It is typically used to provide a String conversion for a class that implements CustomStringConveritble
for printing purposes. If you truly want to override description
in your subclass, you should do it as follows:
override var decsription: String {
return "\(numberOfWheels) wheels, more is good"
}