AFNetworking 迁移 Swift

AFNetworking Migration Swift

我正在尝试使用 AFNetworking 向我自己的服务器发出 HTTP POST 请求:

let manager = AFHTTPSessionManager()
    manager.post(
        URL,
        parameters: params,
        success:
        {
            (operation, responseObject) -> Void in

            if let response = responseObject as? [String: String] {
                let alert = UIAlertController(title: response["status"],
                                              message: response["message"],
                                              preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default) {_ in })
                self.present(alert, animated: true)
            }
        },
        failure:
        {
            (operation, error) -> Void in
            self.handleError(error as NSError)
        })

但是,我收到以下错误:

post(_:parameters:success:failure:) is deprecated

我搜索了 AFNetworking 迁移指南,但它似乎只在 Objective-C 中提供了说明,而不是 Swift。我在 Swift 中找不到新的 post 方法。有任何想法吗?谢谢!

如果您的项目是用 Swift 编写的,为什么不使用 Alamofire

正如 @luke-barry 解释的那样:

AFNetworking and Alamofire are by the same people (the Alamofire Software Foundation), Alamofire is their Swift version whereas AFNetworking is the Objective-C version.

Feature wise they are the same.

有关详细信息,请参阅

希望对您有所帮助!

post(_:parameters:success:failure:) is deprecated

非弃用版本增加了progress:。所以在 Swift 中你使用 post(_:parameters:progress:success:failure:) 作为这个例子:

manager.post(URL, parameters: params,
                    progress: nil,
                     success: { (operation, responseObject) in },
                     failure: { (operation, error) in })