iOS Closures in Swift Building Standard Library Functions Using Flat Map

type annotation: ([Int]) ?

Hi, I am having trouble understanding the type of $0:

let numbers = [[1,2,3], [4,5,6], [7,8,9]]
let numbersFlatMap = numbers.flatMap { return $0 }

where $0 is declared as a constant: let $0: ([Int])

Jhoan Arango
Jhoan Arango
Treehouse Moderator 12,197 Points

Omar,

Would you please give me more context on the question ?

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,803 Points

The parentheses are added around any type that is passed into a closure. They make no difference to the code and can be ignored.

If you look at the numbers value, you will see it has a type [[Int]], or an array of an array of ints. .flatMap takes each element of the parent array and passes it into a closure. This is type [Int], or an array of ints.

Hope this answers your question.

Hi Jhoan, thanks for your reply ! I was trying to understand the closure shorthand syntax by writing it in plain syntax.

let numbersFlatMap = numbers.flatMap({ (value: ([Int])) in 
     return value 
})

I have no idea why the type of value is written as an array inside a pair of parenthesis :/