gpt4 book ai didi

Swift 4 减少问题 : Cannot use mutating member on immutable value: function call returns immutable value

转载 作者:行者123 更新时间:2023-11-28 14:07:20 31 4
gpt4 key购买 nike

我被困在一个

Cannot use mutating member on immutable value: function call returns immutable value

这段代码错误:

var bagsToScan: [BagQR] {
var initial: [Bag] = []
return TaskManager.shared.tasks.value
.filter({ $0.state == .ready })
.reduce(initial) { (acc, task) in
var bagQR = task.bags.filter({
!tasksChecked.contains(BagQR(bag: $0, taskName: task.name).taskName)
}).map({
BagQR(bag: $0, taskName: task.name)
})

acc.append(contentsOf: bagQR)
}.sort({ $0.bag.scanIndex < $1.bag.scanIndex })
}

错误打印在 reduce(initial) { (acc, task) in 行中。感谢您的帮助!

最佳答案

使用reduce(into:_:)

.reduce(into: initial) { (acc, task) in
//...

这将使 acc 可变。

并使用 sorted(by:)关于 reduce 的结果。

.sorted(by: { $0.bag.scanIndex < $1.bag.scanIndex })

关于Swift 4 减少问题 : Cannot use mutating member on immutable value: function call returns immutable value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877027/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com