【问题标题】:Nested closures does not like argument list嵌套闭包不喜欢参数列表
【发布时间】:2014-09-24 16:32:21
【问题描述】:

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

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

【问题讨论】:

    标签: uiview swift closures uianimation completionhandler


    【解决方案1】:

    问题是闭包隐式返回这个表达式的结果:

    self?.warningLabel.alpha = 1.0
    

    但闭包本身被声明为返回Void

    添加明确的return 应该可以解决问题:

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

    【讨论】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多