Intermediate Swift 3 - Part 2: Closures | Ray Wenderlich

In this Intermediate Swift 3 video tutorial you'll learn the basics of closures, how to define, and pass them into your code.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3536-intermediate-swift-3/lessons/2

here is the Swift 3 version of Challenge Closure for printNumbersInReverse:

var printNumbersInReverse: ([Int]) β†’ Void = {(numbers: [Int]) β†’ Void in
for number in numbers.reversed(){
print(number)
}
}

Nice job on that one!

In the solution playground the printNumbersInReverse closure should have the following in line 38:

print(numbers[counter-index])

Otherwise the output is not reversed.

Thanks for the heads up … I’ve updated the files with the correct solution.

In the example of the trailing enclosure in the video (at 03:59), it’s got this as the original form of the enclosure:

operateOnNumbers(10, 5, operation: { $0 * $1 })

– and this as the trailing enclosure:

operateOnNumbers(10, 5) {
$0 + $1
}

Is that addition operator in the trailing enclosure a typographical error? Should it be a multiplication operator?

Hi Brian in video at (3:49) you have written

operateOnNumbers(10, 5, operation: { $0 * $1 })

Can you please tell , how to use it in a playground as playground is not accepting this code.

func operateOnNumbers(_ a: Int, _ b: Int, operation:(Int, Int) β†’ Int) {
operation(a, b)
}