Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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}