目标:创建一个协议,允许对符合协议的结构体进行属性的惰性计算,并且将这些结构体的属性添加到一个数组中。这种计算是密集的,应该只执行一次,因此需要使用lazy
关键字。
所以,经过大量阅读(例如:Swift Struct with Lazy, private property conforming to Protocol)和试错(除非它确实涉及到这个完全相同的情况,否则请不要将此标记为重复),我想出了以下可行的方法:
import Foundation
protocol Foo {
var footype: Double { mutating get }
func calculateFoo() -> Double
}
struct Bar: Foo {
private lazy var _footype: Double = {
let value = calculateFoo()
return value
}()
var footype: Double {
mutating get {
return _footype
}
}
func calculateFoo() -> Double {
print("calc")
return 3.453
}
}
在游乐场中测试:
var bar = Bar()
print(bar.footype)
print(bar.footype)
输出结果为:
calc
3.453
3.453
目前为止,一切都很顺利。
现在,我想创建一个Bar
数组并添加footype
属性:
var bar1 = Bar()
var bar2 = Bar()
var bar3 = Bar()
var bars = [bar1, bar2, bar3]
print(bars.map { $0.footype }.reduce(0.0, +))
我遇到了以下错误:
Cannot use mutating getter on immutable value: '$0' is immutable
找到了很多关于此错误的信息,但是我不知道该如何解决。一种方法是使用 class
而不是 struct
,但这与代码的其他部分不兼容。
我尝试的东西是否可行?
lazy
属性实现,您有什么评论吗? - koencalculateFoo
放在协议中,因为它听起来像是实现细节。你可以在代码审查中发布此内容以获得更多反馈。 - Sweeper