dMoving between different Xcode 6.x version recently, it's been difficult to keep our Swift code stable because of compilation errors. One problem in particular I don't understand relates to animateWithDuration()
.
We have some code that was written in Xcode 6.3 beta / Swift 1.2 and I'm trying to get it working in Xcode 6.2 / Swift 1.1.
This was the original code:-
// Xcode 6.3 / Swift 1.2
UIView.animateWithDuration(0.25, animations: { _ in
self.detailsView?.alpha = 0
self.detailsView?.transform = CGAffineTransformMakeTranslation(0, -64);
}, completion: { finished in
self.detailsView?.hidden = true
})
This fails to compile under Swift 1.1 with the obscure error error: cannot invoke 'animateWithDuration' with an argument list of type '(FloatLiteralConvertible, animations: (($T3) -> ($T3) -> $T2) -> (($T3) -> $T2) -> $T2, completion: (($T5) -> ($T5) -> $T4) -> (($T5) -> $T4) -> $T4)'
After some digging around I realised it was due to the return type of the completion:
closure, and I found a suggestion to put in a return
statement, which duly fixes it:-
// Xcode 6.2 / Swift 1.1
UIView.animateWithDuration(0.25, animations: { _ in
self.detailsView?.alpha = 0
self.detailsView?.transform = CGAffineTransformMakeTranslation(0, -64);
}, completion: { finished in
self.detailsView?.hidden = true
return // this fixes it!
})
But I'm left confused by this. I appears the assignment self.detailsView?.hidden = true
creates an implicit non-void (presumably Bool) return type for the closure. Indeed, removing the line altogether allows the code to compile.
But specifying the closure type explicitly does not fix the problem:-
// Xcode 6.2 / Swift 1.1
UIView.animateWithDuration(0.25, animations: { _ in
// ...
}, completion: { (finished:Bool) -> () in
self.detailsView?.hidden = true
}
... which really surprises me. To me this says we've declared a closure that will return void, and the compiler is overriding that when it sees the innocuous Boolean assignment and saying "No, that closure returns a Bool."
This seems broken, and Xcode 6.2 is no longer a beta. Is this a bug in Swift?