Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
14,575 Points

Omar,

Would you please give me more context on the question ?

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 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 :/