简体   繁体   English

Alamofire 5:“结果”类型的值<data, aferror> '没有成员'值'</data,>

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

Is the a new error in Alamofire 5? Alamofire 5 中的新错误是什么? as this wasn't running into bugs last time.因为上次没有遇到错误。 Below are the code which are done.下面是完成的代码。 Anyone who used Alamofire facing this?任何使用 Alamofire 的人都面临这个问题?

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

You have to extract the result value as below,您必须提取result值如下,

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

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?)
          }
      }
 }

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

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