闭包参数列表的上下文类型需要1个参数,但指定了4个 [英] Contextual type for closure argument list expects 1 argument, but 4 were specified
本文介绍了闭包参数列表的上下文类型需要1个参数,但指定了4个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已升级到Xcode 7,并且正在使用Alamofire来管理API调用,但出现此错误:
I upgraded to Xcode 7 and I am using Alamofire to manage API calls and I am getting this error:
闭包参数列表的上下文类型需要1个参数,但指定了4个参数"
'Contextual type for closure argument list expects 1 argument, but 4 were specified'
对于以下代码:
static func loginWithEmail(email: String, password: String, response: (token: String?) -> ()) {
let urlString = baseURL + ResourcePath.login.description
let parameters = [
"email": email,
"password": password
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { (_, _, data, _) -> Void in
let json = JSON(data!)
let token = json["token"].string
response(token: token)
}
}
错误是指以下行:
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { (_, _, data, _) -> Void in
我是Swift闭包的新手,不知道是否必须嵌套值才能使其有效.
I am new to swift closures and do not know if I have to nest the values in order for it to be valid.
非常感谢您的帮助.
推荐答案
闭包采用类型为Response< AnyObject,NSError>的单个参数,因此您的代码应更像这样.
The closure takes a single parameter of type Response<AnyObject, NSError> so your code should look more like this.
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON { response in
let json = JSON(response.data!)
let token = json["token"].string
response(token: token)
}
这篇关于闭包参数列表的上下文类型需要1个参数,但指定了4个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文