Fixing WWDC closure code
If you try code about closures for WWDC 2014 session 403 you will see this error
class TemperatureNotifier {
var onChange: (Int) -> Void = {}
var currentTemp = 72
}
'Int' is not a subtype of '()'
The problem is that onChange closure is taking 1 Int
parameter, and it has to be specified in the body of the closure. There is a simple fix to it.
You have to declare variable names in closure.
class TemperatureNotifier {
var onChange: (Int) -> Void = {x in}
var currentTemp = 72
}
Or use shorthand style and make explicit return, to return void
class TemperatureNotifier {
var onChange: (Int) -> Void = {$0; return}
var currentTemp = 72
}
Closures are really great feature of Swift and it provides awesome functional programming style. But as I can see now they are a bit broken. Hope it will be solved soon