【发布时间】:2018-10-06 07:28:25
【问题描述】:
我看过这里:Swift 3 - Reduce a collection of objects by an Int property 和这里:https://medium.com/@abhimuralidharan/higher-order-functions-in-swift-filter-map-reduce-flatmap-1837646a63e8,但我无法减少工作。编辑:也许是因为他们改变了代码在 swift 4 中的编译方式(我正在使用)https://stackoverflow.com/a/46432554/7715250
这是因为我收到此错误:
上下文闭包类型 '(_, Test) -> _' 需要 2 个参数,但在闭包体中使用了 1 个
这是我的课:
class Test {
let amountLeftToDownload: Int
let amountDownloaded: Int
init(amountLeftToDownload: Int, amountDownloaded: Int) {
self.amountLeftToDownload = amountLeftToDownload
self.amountDownloaded = amountDownloaded
}
}
我有一个数组(进度)。我想获取下载总数,即每个测试实例的 amountLeftToDownload + amountDownloaded。我试过这个:
let totalDownloadsToProcess = progress.reduce(0) {$0.amountLeftToDownload, $0.amountDownloaded }
我将逗号替换为 + 和其他一些东西,但它不起作用。
【问题讨论】:
标签: swift