Json Alamofire 4,Swift 3:无法返回状态码
这会给出一个错误:“转义闭包只能捕获inout…” 我怎样才能修好它Json Alamofire 4,Swift 3:无法返回状态码,json,swift,http,post,alamofire,Json,Swift,Http,Post,Alamofire,这会给出一个错误:“转义闭包只能捕获inout…” 我怎样才能修好它 func check_info(tag: String, info: String, code: inout Int) { Alamofire.request( URL + "/api/test_information/", method: .post, parameters: [tag: info], encoding: JSONEncoding.de
func check_info(tag: String, info: String, code: inout Int) {
Alamofire.request(
URL + "/api/test_information/",
method: .post,
parameters: [tag: info],
encoding: JSONEncoding.default
).responseString { response in
code = (response.response?.statusCode)!
}
}
已编辑:如果要返回状态代码,请使用此回调函数。带有的回调函数会在收到响应后立即返回状态代码
func check_info(tag: String, info: String, statusCode: @escaping (String)->Void){
Alamofire.request(URL + "/api/test_information/", method: .post, parameters: [tag: info], encoding: JSONEncoding.default).responseJSON { response in
statusCode((response.response?.statusCode)!)
}
}
要命名它,请使用:
check_info(tag: "yourtag", info: "yourInfo){
statusCode in
print(statusCode)
}
谢谢你的回答!但是我需要在另一个例子中使用这个值function@MarkYankovsky现在检查一下,使用这个回调函数应该可以工作。我自己没有测试过,但这是我使用它的方式Hanks,但我怎么能称它为func?我从来没有用过“逃跑”这个词,马基扬科夫斯基对不起,我完全忘了那个部分。刚刚编辑了我的答案。使用一种完成块捕捉转义值。当Alamofire请求完成时,将调用该完成块。