我是Swift新手,遇到了一些麻煩。我試圖從我的要求中獲得價值。我可以打印它作爲封閉,但我想在VC上獲得字符串並使用它,但功能登錄。我無法弄清楚如何從封閉返回。swift 3調用函數完成關閉作爲回報
功能的關閉:在VC
class Json {
var loginToken = ""
public func login(userName: String, password: String) -> (Any){
let loginrequest = JsonRequests.loginRequest(userName: userName, password: password)
return makeWebServiceCall(urlAddress: URL, requestMethod: .post, params: loginrequest, completion: { (JSON : Any) in
//let jsons = JSON
print("\(JSON)")
})
}
private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) ->()) {
Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success(let data):
if let jsonData = response.result.value {
completion(jsonData)
}
case .failure(let error):
if let data = response.data {
let json = String(data: data, encoding: String.Encoding.utf8)
completion("Failure Response: \(json)")
}
調用函數:
let retur = Json()
let rezultatas = retur.login(userName: "root", password: "admin01")
print(rezultatas)
你不能從封閉返回。 – ebby94
請參閱我對上一個問題的回答:http://stackoverflow.com/a/41889841/4063602 – pbodsk
爲什麼要從登錄方法返回值? –