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

Viewed 4129

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
        }
1 Answers

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
}
Related