soleil
soleil

Reputation: 13073

missing argument label 'combine:' in call

Getting the error shown below:

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

In all examples I've seen of reduce, the combine label is not used:

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

So what am I doing wrong?

Upvotes: 0

Views: 467

Answers (2)

sbooth
sbooth

Reputation: 16966

You can omit the label if you use a trailing closure:

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 }

With a trailing closure the closure is supplied to the function after the final ). If you don't want to use a trailing closure you need to add the argument label:

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

Upvotes: 4

Ivica M.
Ivica M.

Reputation: 4823

Use the trailing closure syntax:

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

Upvotes: 0

Related Questions