嵌套闭包不喜欢参数列表 [英] Nested closures does not like argument list

查看:111
本文介绍了嵌套闭包不喜欢参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据自定义控件的完成处理程序,UIView需要更改警告标签:

  voucherInputView.completionHandler = { [weak self](成功:Bool) - >无效

self?.proceedButton.enabled = success
self?.warningLabel.alpha = 1.0

如果成功
{
self ?.warningLabel.text =您输入的代码正确
self?.warningLabel.backgroundColor = UIColor.greenColor()
}
else
{
self? .warningLabel.text =您输入的代码不正确
self?.warningLabel.backgroundColor = UIColor.orangeColor()
}


UIView.animateWithDuration(NSTimeInterval (1.0),animations:{() - > Void in
self?.warningLabel.alpha = 1.0
})

最后的动画块在窗体中显示错误。

 无法调用animateWithDuration ',类型为(NSTimeInterval)的参数列表,animations:() - > Void)'

如果我把这个调用到完成闭包之外的某个地方。

$问题是,闭包是隐式返回此表达式的结果:



<$ p

$ p> self?.warningLabel.alpha = 1.0

被声明为返回 Void



添加显式 >应该解决问题:

  UIView.animateWithDuration(NSTimeInterval(1.0),animations:{() - & b $ b self?.warningLabel.alpha = 1.0 
return
})


A UIView needs to change a warning label depending on the completion handler of a custom control:

    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
        })

The final animation block shows an error in the form.

Cannot invoke 'animateWithDuration' with an argument list of type '(NSTimeInterval), animations: ()-> Void)'

If i call this somewhere outside of the completion closure it works.

解决方案

The problem is that the closure is implicitly returning the result of this expression:

self?.warningLabel.alpha = 1.0

but the closure itself is declared as returning Void.

Adding an explicit return should resolve the problem:

UIView.animateWithDuration(NSTimeInterval(1.0), animations: { ()-> Void in
    self?.warningLabel.alpha = 1.0
    return
})

这篇关于嵌套闭包不喜欢参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆