简体   繁体   English

“结果”类型的 Alamofire 值<any, aferror> ' 没有成员 'isSuccess' (Swift 5)</any,>

[英]Alamofire Value of type 'Result<Any, AFError>' has no member 'isSuccess' (Swift 5)

I have a simple procedure, which I used in my earlier version ( Swift 4 ).我有一个简单的程序,在我的早期版本中使用过( Swift 4 )。 I have now updated to the new version ( Swift 5, Alamofire 5.0.0-rc.2 ), as I am still beginner I got problems with this.我现在已经更新到新版本( Swift 5, Alamofire 5.0.0-rc.2 ),因为我还是初学者,所以我遇到了问题。 Some code parts I could already rewrite.我已经可以重写一些代码部分。

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 is not possible. response.result.isSuccess是不可能的。 How can I reuse this part?我怎样才能重用这部分?

Error: Value of type 'Result' has no member 'isSuccess'错误:“结果”类型的值没有成员“isSuccess”

As a possible solution, I will test the above solution.作为一种可能的解决方案,我将测试上述解决方案。

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
        }

Result is containing .success or .failure case.结果包含.success.failure案例。

So you should treat isSuccess like this所以你应该这样对待isSuccess

switch response.result {
   case .success(let value):
//success, do anything
   case .failure(let error): 
//failure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM