3

Alamofire在我的一个应用程序中使用过。现在应用程序需要更新。所以我Alamofire用新版本更新了 pod。

现在我不知道如何Alamofire在 swift 5 中使用新版本的Alamofire. 我试过了,但它抛出错误。我搜索了很多,但没有找到任何解决方案。

谁能帮我打电话Alamofire

错误:

  1. 协议类型“Any”不能符合“Encodable”,因为只有具体类型才能符合协议
  2. “结果”类型的值没有成员“isFailure”
  3. “结果”类型的值没有成员“错误”

一个

4

1 回答 1

3

我创建了一个示例应用程序只是为了提供一个想法

struct Article: Codable {
    let firstName: String
    let lastName: String
    let email: String
}

struct User: Encodable {
    let name: String
    let password: String
}
// data model
let userStruct = User(name: "test", password: "pass")

// data dictionary
let userDictionary = ["name": "test", "password": "pass"]

func getData() {
        let headers: HTTPHeaders = ["Access-Token-Key": ""]
        let request = AF.request(url,
                   method: .post,
                   parameters: userDictionary,   // or 'userStruct' because both conform to 'Encodable' parameters.
                   encoder: JSONParameterEncoder.default,
                   headers: headers)
        // Receive and decode the server's JSON response.
        request.responseDecodable(of: Article.self) { response in
           switch response.result {
           case let .success(result):
              // the decoded result of type 'Article' that you received from the server.
            print("Result is: \(result)")
           case let .failure(error):
              // Handle the error.
            print("Error description is: \(error.localizedDescription)")
           }
        }
    }

如果你想使用parameters类型[String: Any]然后使用Alamofire version 4.9.1. 我在示例应用程序中使用的版本是5.0.0-rc.3

于 2019-12-24T19:30:17.387 回答