【发布时间】:2019-10-07 10:52:08
【问题描述】:
我有一个简单的程序,在我的早期版本 (Swift 4) 中使用过。我现在已经更新到新版本(Swift 5,Alamofire 5.0.0-rc.2),因为我还是初学者,我遇到了这个问题。我已经可以重写一些代码部分。
let headers: HTTPHeaders = [
"SubscriptionKey": SubscriptionKey,
"Host": HostName
]
AF.request(URL(string: userInfoUrl)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers)
.validate()
.responseJSON { response in
guard response.result.isSuccess else {
log.error("Error requesting user ID: \(String(describing: response.result.error))")
completion(false, nil)
return
}
guard let json = response.result.value as? [String: Any], let userId = json["userId"] as? String else {
log.error("Malformed result from API call.")
completion(false, nil)
return
}
response.result.isSuccess 是不可能的。如何重复使用这部分?
错误:“Result”类型的值没有成员“isSuccess”
作为一个可能的解决方案,我将测试上述解决方案。
AF.request(URL(string: userInfoUrl)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers)
.validate()
.responseJSON { response in
switch response.result{
case .success(let value):
//succcess, do anything
case .failure(let error):
log.error("Error requesting user ID: \(String(describing: response.error))")
completion(false, nil)
return
}
【问题讨论】: