我正在使用Alamofire向HTTP服务器发送登录请求。它返回一个JSON响应。我的问题是主线代码在.responseJSON之前完成。在从函数返回之前,我该如何等待返回响应?
var ret: Bool = true
Alamofire.request(
URL(string: "http://localhost:8081/login/iPhone")!,
method: .post,
parameters: ["email":"test@test.test", "password":"test", "uuid":String(describing: UIDevice.current.identifierForVendor!.uuidString)],
headers: [:])
.validate()
.responseJSON{(response) -> Void in
do {
guard response.result.isSuccess else {
throw FieldError.fetchError(responseError: response.result.error)
}
guard let value = response.result.value as? [String: Any],
let status = value["status"] as? String,
let message = value["message"] as? String else {
throw FieldError.messageFormatError
}
switch status {
case "Login suggess":
break
default:
throw FieldError.fieldServerError(status: status, message: message)
}
} catch {
ret = false
debugPrint(error)
}
}
return ret
答案 0 :(得分:0)
您始终可以使用全局
typealias DownloadComplete = () -> ()
接下来在func中与你同在的json
func yourFunc(completed: @escaping DownloadComplete)
下载后使用
completed()