2017-05-26 69 views
0

我試圖讓使用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方法。有任何想法嗎?謝謝!

回答

0

如果您的項目是用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

希望這可以幫助你!

1
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

謝謝,這個工作! –