AFNetworking迁移斯威夫特

问题描述:

我试图让使用AFNetworking HTTP POST请求到我自己的服务器:AFNetworking迁移斯威夫特

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.

更多信息请参见Swift Alamofire VS AFNetworking

希望这可以帮助你!

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

谢谢,这个工作! –