简体   繁体   中英

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 ). 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. 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. How can I reuse this part?

Error: Value of type 'Result' has no member '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.

So you should treat isSuccess like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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