M. Ying
M. Ying

Reputation: 197

AFNetworking Migration Swift

I'm trying to make an HTTP POST request to my own server using 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)
        })

However, I'm getting the following error:

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

I searched the AFNetworking migration guide but that seems to only provide instructions in Objective-C, not Swift. I cannot find the new post method in Swift. Any ideas? Thanks!

Upvotes: 0

Views: 738

Answers (2)

Cœur
Cœur

Reputation: 38667

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

The non-deprecated version adds progress:. So in Swift you use post(_:parameters:progress:success:failure:) as in this example:

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

Upvotes: 1

Anh Pham
Anh Pham

Reputation: 2118

If your project is written in Swift, why don't you use the Alamofire?

As @luke-barry explained:

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.

See Swift Alamofire VS AFNetworking for more information.

Hope this helps you!

Upvotes: 0

Related Questions