一个UIView需要根据自定义控件的完成处理程序更改警告标签:
voucherInputView.completionHandler = {[weak self] (success: Bool) -> Void in
self?.proceedButton.enabled = success
self?.warningLabel.alpha = 1.0
if success
{
self?.warningLabel.text = "Code you entered is correct"
self?.warningLabel.backgroundColor = UIColor.greenColor()
}
else
{
self?.warningLabel.text = "Code you entered is incorrect"
self?.warningLabel.backgroundColor = UIColor.orangeColor()
}
UIView.animateWithDuration(NSTimeInterval(1.0), animations:{ ()-> Void in
self?.warningLabel.alpha = 1.0
})
最后的动画块显示表单中的错误。
Cannot invoke 'animateWithDuration' with an argument list of type '(NSTimeInterval), animations: ()-> Void)'
如果我在完成闭包之外的某个地方调用它,它就可以工作。
()->Void
替换为_
,并使用; return
将返回附加到同一行。此外,您可以写;()
而不是单行return
。 :) - BastiBenreturn
关键字的闭包时,闭包会自动返回最后一个语句。这样做是为了让array.sort {$0.index < $1.index}
起作用。如果你的最后一个语句是可选类型的赋值,它将返回Void?
。如果你让 Xcode 推断类型,你可以看到这一点:() -> ()?
。空的return
语句隐式地返回Void
(非可选类型)。有趣的事实是:Void
是()
, 空元组的别名,如果你不输入return
语句,所有函数都会返回它。(1/2) - vrwimVoid
作为最后一行的结果,因为它们不需要猜测任何返回值。所有的返回值都必须是明确的。 - vrwimreturn
后面加;
或者();
?总体而言,加上; return
比单纯的return
需要输入更多字符吗?如果Antonio能回答就更好了。提前感谢。 - Unheiligself?.warningLabel.alpha = 1.0; return
。 - Antonio