One issue with Swift Closure

Here I have a simple snippet to use a simple animation with my specific UIView.

UIView.animateWithDuration(0.1) { [weak self] in

    self?.popOverView.center = gesture.locationInView(self?.view)
}

      

Here [weak me] is to avoid the reference loop, and I also use trailing closure for code only. Howerver, the compiler is unhappy with this and gives me the wrong message.

Cannot invoke 'animateWithDuration' with an argument list of type '(FloatLiteralConvertible, () -> () -> $T2)'

      

What does $ T2 mean? And the odd thing is that when there are two or more statements in the closure body, it compiles correctly.

UIView.animateWithDuration(0.1) { [weak self] in
    println()
    self?.popOverView.center = gesture.locationInView(self?.view)
}

      

And I know that if there is only one statement in the closing body, it automatically returns.

+3


source to share


1 answer


The single body of a statement in a closure has an implicit return, so what happens is that the compiler tries to establish the result of this statement:

self?.popOverView.center = gesture.locationInView(self?.view)

      

as a return value. You can fix this by adding an explicitreturn



self?.popOverView.center = gesture.locationInView(self?.view)
return

      

This only happens for single statements, so it works correctly in your second case

More information: Implicit returns from single-expression closures

+5


source







All Articles