惰性变量

控制流 / 其他语句

class WeatherMap {
    let city = "Moscow"
}

class WeatherService {
    //值在首次访问时创建
    lazy var map = WeatherService.load()

    static func load() -> WeatherMap {
        print("2. Creating WeatherMap")
        return WeatherMap()
    }
}

//全局变量本身就是惰性的,
//所以不允许对其使用 "lazy"
let service = WeatherService()

print("1. Before accessing the map")
print("3. Got the map for", service.map.city)
print("4. Got it again for", service.map.city)
//WeatherMap 只创建一次