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

标签 ios swift alamofire

Alamofire 5 中的新错误是什么?因为上次没有遇到错误。下面是完成的代码。任何使用 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)
    }
  }
}

最佳答案

您必须提取 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)
        }
    }
}

关于ios - Alamofire 5 : Value of type 'Result<Data, AFError>' has no member 'value' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58177841/

相关文章:

android - 我们可以为 Android 和 iOS 应用程序使用 Square Checkout 流程吗?

ios - 程序员如何设计他们的 iOS 应用程序

ios - 如何获得 AvPlayer 持续时间 - Swift

ios - 使用 alamofire 下载不完整的文件

swift2 - 在 Alamofire 中使用 FORM DATA

ios - 如何通过 SwiftyJson 和 Alamofire 发布嵌套的 json?

iphone - 如何在设备重启时在后台运行 ios 应用程序?

ios - 从自定义单元格调用 ActivityViewController

ios - ViewModel 类能否按照 MVVM 符合 iOS 中的 UITableviewDelegate 和 UITableViewDataSource

ios - iOS siri快捷键Swift