Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire 5: Value of type 'Result<Data, AFError>' has no member 'value'

Is the a new error in Alamofire 5? as this wasn't running into bugs last time. Below are the code which are done. Anyone who used Alamofire facing this?

import Foundation
import Alamofire

class MyAppService {
static let shared = MyAppService()
let url = "http://127.0.0.1:5000"

private init() { }

func getCurrentUser(_ completion: @escaping (SomeRequest?) -> ()) {
    let path = "/somePath"
    AF.request("\(url)\(path)").responseData { response in
        if let data = response.result.value { //error shown here (Value of type 'Result<Data, AFError>' has no member 'value')
            let contact = try? SomeRequest(protobuf: data)
            completion(contact)
        }
        completion(nil)
    }
  }
}
like image 608
L.William Avatar asked Oct 01 '19 03:10

L.William


2 Answers

You have to extract the result value as below,

func getCurrentUser(_ completion: @escaping (SomeRequest?) -> ()) {
    let path = "/somePath"
    AF.request("\(url)\(path)").responseData { response in
        switch response.result {
        case .success(let value):
            print(String(data: value, encoding: .utf8)!)
            completion(try? SomeRequest(protobuf: value))
        case .failure(let error):
            print(error)
            completion(nil)
        }
    }
}
like image 174
Kamran Avatar answered Sep 17 '22 09:09

Kamran


You can also extract response it this way

AF.request(url, method: HTTPMethod.get, parameters: param as? Parameters)
.responseJSON { response in
      if let JSON = response.value {
          if response.response?.statusCode == 200{
              completionHandler(JSON as AnyObject?, nil)
          }else if(response.response?.statusCode == 401){
                completionHandler(JSON as AnyObject?, nil)
          }
      }
      else{
          if response.response?.statusCode == 401 {
              SVProgressHUD.showInfo(withStatus: "Request timed out.")
          }
          else {
              completionHandler(nil,response.error as NSError?)
          }
      }
 }
like image 37
Paresh Mangukiya Avatar answered Sep 21 '22 09:09

Paresh Mangukiya