2017-02-15 27 views
0

这给出了一个错误:“逃离封闭只能捕捉INOUT ......”Alamofire 4,斯威夫特3:不能返回的StatusCode

我怎样才能解决这个问题?

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

} 

回答

0

编辑:如果你想返回状态码,使用这个回调函数。只要您收到回复,该回叫就会返回状态代码。

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

谢谢您的回答!但我需要在另一个函数中使用此值 –

+0

@MarkYankovsky现在检查,使用此回调函数它应该工作。没有自己测试,但这就是我使用它的方式 –

+0

谢谢,但我怎么称呼这个功能?我从未使用过@escaping? –