Reputation: 961
I have some strange stuff going on: trying to track if a share action was cancelled in UIAlertAction handler with FLurry Analytics SDK. The code should basically look like that:
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(action:UIAlertAction!) -> Void in
Flurry.logEvent("Share Cancelled")
}
)
But compiler shows an error "Extra argument "title" in call..." and highlights "Cancel" in red. Though if I add any variable declaration or simple function like println() than there is no error! i.e. this code is compiled correctly and considered to be working:
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(action:UIAlertAction!) -> Void in
let somevar = 0
Flurry.logEvent("Share Cancelled")
}
)
Have anyone faced a thing like that? Maybe it's a Swift or Flurry bug?
Upvotes: 2
Views: 1085
Reputation: 9039
For sake of resolution, from my previous comment, Swift will try to infer the return type from a closure with a single statement. If you are trying to define a closure with a single statement that has a return value, then Swift will assume that to be the return value for your closure.
Since you noted that logEvent returns a value, you will need to explicitly return nothing at all, to create the proper closure.
For what it's worth, this appears to be changed / fixed in 6.3, as the following code now executes:
func foo() -> Int {
return 1
}
func bar(() -> ()) {
println("Hello from bar")
}
bar({ foo() })
In general, the way to debug ambiguous errors like this, is to break the statement into multiple lines, explicitly defining expected types at each stage, and watch where it breaks.
Upvotes: 0