The compiler complains about
var completionHandler: (Float)->Void = {}
since the right side is not a closure of the corresponding signature, i.e. a closure that takes a float argument. The following will assign the closure to a "do nothing" completion handler:
var completionHandler: (Float)->Void = { (arg: Float) -> Void in }
and it can be reduced to
var completionHandler: (Float)->Void = { arg in }
due to automatic type inference.
But , what you probably want is that the completion handler is initialized to nil in the same way that the Objective-C instance variable is initialized to nil . In Swift, this can be implemented using the option:
var completionHandler: ((Float)->Void)?
Now the property is automatically initialized to nil ("no value"). In Swift, you must use an optional binding to verify the completion handler matters
if let handler = completionHandler { handler(result) }
or optional chain:
completionHandler?(result)
Martin R Jul 07 '14 at 7:09 2014-07-07 07:09
source share