Alamofire在Swift 5中的请求

3

我在一个应用程序中使用了 Alamofire。现在需要更新应用程序,所以我已经使用新版本更新了 Alamofire pods。

现在我不知道如何在 Swift 5 中调用具有新版本的 Alamofire 请求。我尝试过了,但会抛出错误。我搜索了很多,但没有找到任何解决方法。

有人可以帮我使用 Alamofire 进行调用吗?

错误:

  1. 协议类型“Any”无法符合“Encodable”,因为只有具体类型才能符合协议
  2. 类型“Result”的值没有成员“isFailure”
  3. 类型“Result”的值没有成员“error”

A

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

如果您想使用类型为[String: Any]的参数,请使用Alamofire 4.9.1版本。示例应用程序中使用的版本是5.0.0-rc.3


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接