缺少参数标签'合并:'在电话中

时间:2015-02-24 22:36:21

标签: arrays swift

获得如下所示的错误:

var products = [
    ("Kayak", "A boat for one person", "Watersports", 275.0, 10),
    ("Lifejacket", "Protective and fashionable", "Watersports", 48.95, 14)]

let stockTotal = products.reduce(0,{(total, product) -> Int in return total + product.4}); //missing argument label 'combine:' in call

在我看过reduce的所有例子中,都没有使用联合收割机标签:

let numbers = Array(1...10)
.reduce("numbers: ") {(total, number) in total + "\(number) "}

那么我做错了什么?

2 个答案:

答案 0 :(得分:4)

如果使用尾随闭包,则可以省略标签:

var products = [
    ("Kayak", "A boat for one person", "Watersports", 275.0, 10),
    ("Lifejacket", "Protective and fashionable", "Watersports", 48.95, 14)
]

let stockTotal = products.reduce(0) { $0 + $1.4 }

使用尾随闭包,在最终)之后将闭包提供给函数。如果您不想使用尾随闭包,则需要添加参数标签:

let stockTotal2 = products.reduce(0, combine: { $0 + $1.4 })

答案 1 :(得分:0)

使用尾随闭包语法:

let stockTotal = products.reduce(0) {(total, product) -> Int in return total + product.4}