Swift 属性的多个吸气剂?

Multiple Getters for Swift Properties?

在 Swift 3 中有没有什么方法可以让 class 和计算出来的 属性 第一次在计算上很昂贵,但之后保持不变,有单独的 getters (一个初始的,然后是每个后续请求的另一个)?即

class Example {
    var computationallyIntensive: String? {
        return try? String(contentsOf: unlistedFile, encoding: .utf8)
    }
}

我知道初始化器,但是这个 属性 不需要在创建 class 时初始化。

理想情况下,第二次调用 return 比第一次调用快得多:

let test = Example()
if test.computationallyIntensive == "base case" {
    print(test.computationallyIntensive)
}

使用 Lazy Stored Property:

lazy var computationallyIntensive: String? = computeComputationallyIntensive()

func computeComputationallyIntensive() -> String?  {
    return try? String(contentsOf: unlistedFile, encoding: .utf8)
}

computeComputationallyIntensive 的调用(如果有)将在对 computationallyIntensive 的第一次调用期间发生。