Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uiview 嵌套闭包不喜欢参数列表_Uiview_Swift_Closures_Uianimation_Completionhandler - Fatal编程技术网

Uiview 嵌套闭包不喜欢参数列表

Uiview 嵌套闭包不喜欢参数列表,uiview,swift,closures,uianimation,completionhandler,Uiview,Swift,Closures,Uianimation,Completionhandler,UIView需要根据自定义控件的完成处理程序更改警告标签: voucherInputView.completionHandler = {[weak self] (success: Bool) -> Void in self?.proceedButton.enabled = success self?.warningLabel.alpha = 1.0 if success { self?.wa

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

如果我在完成闭包之外的某个地方调用它,它会工作。

问题在于闭包隐式返回此表达式的结果:

self?.warningLabel.alpha = 1.0
但是闭包本身被声明为返回
Void

添加显式的
返回应该可以解决问题:

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

Antonio的解决方案也适用于嵌套闭包,比如在UITableViewRowAction处理程序中执行AFNetworking请求

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    let cleanRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Do Stuff", handler: {[weak self](action: UITableViewRowAction!, indexPath: NSIndexPath!) in

        AFHTTPSessionManager(baseURL: NSURL(string: "http://baseurl")).PUT("/api/", parameters: nil, success: { (task: NSURLSessionDataTask!, response: AnyObject!) -> Void in

                // Handle success

                self?.endEditing()
                return
            }, failure: { (task: NSURLSessionDataTask!, error: NSError!) -> Void in

                // Handle error

                self?.endEditing()
                return
        })
        return

    })

    cleanRowAction.backgroundColor = UIColor.greenColor()
    return [cleanRowAction]
}

这为我解决了这个问题,但是有人能解释为什么这种行为对很多人来说是如此奇怪和意外吗?顺便说一句,在您的示例中,您可以用
\
替换
()->Void
,并使用
附加return;将
返回到同一行。此外,您还可以编写
;()
而不是单行
return
:)@badcat当您编写一个没有
return
关键字的闭包时,闭包会自动返回最后一条语句。它这样做
array.sort{$0.index<$1.index}
工作。如果上一条语句是可选类型的赋值,它将返回
Void?
。如果让Xcode推断类型:
()->()?
,则可以看到这一点。空的
return
语句隐式返回
Void
(非可选类型)。有趣的事实:
Void
()
typealias
,空元组,如果不键入
return
语句,所有函数都会返回该元组。(1/2)(2/2)函数可以处理可选的
Void
作为最后一行的结果,因为它们不需要猜测任何
返回值。所有的返回必须是明确的。@badcat写
有什么好处;返回
;()
刚刚结束
返回
?最重要的是,
;return
比return
要键入的字符多?或者如果安东尼奥能回答,请回答。提前谢谢。@Unheilig:您可以在一行中编写闭包-在这种情况下,两条语句必须分开:
self?.warningLabel.alpha=1.0;返回