具有惰性属性的 Swift 协议(protocol) - 不能在不可变值 : '$0' is immutable 上使用变异 getter

标签 swift protocols lazy-loading

目标:创建一个协议(protocol),允许延迟计算符合协议(protocol)的结构的属性,然后为这些结构的数组添加属性。计算量很大,应该只执行一次,因此 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
    }
}

在 Playground 中测试:

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,但这不适用于代码的其他部分。

我想达到的目标有可能吗?

最佳答案

从错误消息中可以看出,$0 是不可变的,因此您不能在其上使用可变成员。

因此,您不能直接遍历 bars。您可以做的是遍历其索引,因为我们知道bars[$0] 可变的:

print(bars.indices.map { bars[$0].footype }.reduce(0.0, +))

关于具有惰性属性的 Swift 协议(protocol) - 不能在不可变值 : '$0' is immutable 上使用变异 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364613/

相关文章:

swift - 我的协议(protocol)定义在另一个 Swift 文件中不可见

html - 如何在 CSS 中延迟加载图像

swift - 如何使用 SwiftUI 制作单选列表

ios - Swift:如何检测 UIView 内的元素?

swift - 更改标签栏颜色(Swift)

ios - 在两个不同的类中对同一文件使用委托(delegate)

ios - 如何实现与另一个类同名的协议(protocol)

Angular 4 ngComponentOutlet 和延迟加载模块

javascript - Ember.js 2.渲染页面后在后台延迟获取

ios - 无法识别带有 UILocalizedIndexedCollat​​ion.current().section(for :collationStringSelector:#selector(getter: UIPreviewAction. title)) 的选择器