我在这个结构体中遇到了问题,在读取“lazy var townSize: Size = {”的行上,但无法确定问题所在。
struct Town {
let region = "South"
var population = 5422
var numberOfStoplights = 4
enum Size {
case Small
case Medium
case Large
}
lazy var townSize: Size = {
switch self.population {
case 0...10000:
return Size.Small
case 10001...100000:
return Size.Medium
default:
return Size.Large
}
}
func printTownDescription() {
print("Population: \(myTown.population), number of stoplights: \(myTown.numberOfStoplights)")
}
mutating func changePopulation(amount: Int) {
population += amount
}
}
population
后来发生变化,townSize
不会自动改变。我猜他想要一个计算属性。 - Rob