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 2 Closure Expressions Using Closure Expressions

Michael Castro
Michael Castro
11,959 Points

Challenge Task 1 of 1 In the editor, we're using the map function on an array of numbers to perform an operation.

What's wrong with what I've done here? I'm using a trailing closure and short hand syntax to double each value

closures.swift
func doubleValue(i: Int) -> Int {
    return i * 2
}

let doubler = doubleValue
let doubledValues = [1,2,3,4].map() { $0 * 2 }

3 Answers

Brandon Maldonado Alonso
Brandon Maldonado Alonso
10,050 Points

I'm not sure if it is still useful for you, but you just have to delete the parenthesis from map like this: let doubledValues = [1,2,3,4].map { $0 * 2 }

carriebarnett
carriebarnett
16,732 Points

This also worked for me:

let doubledValues = [1,2,3,4]  //.map(doubler)

let doubler = doubledValues.map { $0 * 2 }
func double(_ i: Int) -> Int {
    return i * 2
}

print(doubler)
Mina Makar
Mina Makar
5,421 Points

func double(_ i: Int) -> Int { return i * 2 }

let array = [1,2,3,4]

let doubler = array.map { $0 * 2}