本文介绍了Alamofire 5:类型为'Result< Data,AFError>'的值没有成员“值”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alamofire 5是否有新错误?因为上次没有遇到错误。下面是完成的代码。是否有人使用Alamofire来面对这个问题?

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


推荐答案

您必须提取结果值,如下所示,

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

这篇关于Alamofire 5:类型为'Result&lt; Data,AFError&gt;'的值没有成员“值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:17