Project Alamofire conversion to Swift 3.0

Asked

Viewed 204 times

6

I have the following code when I want to make a call to the server:

let mutableURLRequest = NSMutableURLRequest(url: URL)
            mutableURLRequest.httpMethod = "POST"

let parameters = ["SessionID": sessionID]
do {
     mutableURLRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions())
   } catch {}

mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

Alamofire.request(**mutableURLRequest**).responseJSON{

response in if let JSON = response.result.value{}}

Where the text is BOLD is giving me error in this variable

Argument type 'NSMutableURLRequest' does not conform to expected type 'URLRequestConvertible'

yet you give me the following option

Fix-it Insert as! URLRequestConvertible

If I accept the suggestion is put in the code what they say but the error persists and tells to put again and is this infinitely.

Does anyone know how to solve this problem?

  • Just a suggestion. Do you only use the Alamofire for basic orders? Because if that’s the case, the basic use of Urlsession would be sufficient instead of walking with a dependency as large as the Alamofire.

  • @Nunogonçalves, yes I only use the Alamofire for basic orders, I am new to programming in Io and at the time I tried many ways to order from the server and I did not succeed with any. Until finally with Alamofire gave soon. Can you post or give an example of Urlsession? Thanks

  • I added an answer with an example that can be pasted directly onto a playground.

2 answers

2

Swift 3 you must use Urlrequest instead of Nsurlrequest. When it is Nsmutableurlrequest you should also use Urlrequest but as Urlrequest is a Structure and not a class to be able to change its properties you need to declare your object as variable instead of constant. In addition you are passing URL where a URL object should be:

var mutableURLRequest = URLRequest(url: URL(string: "http://www.exemplo.com")!)

1


Not being a direct answer to the question, and to be able to put some code here, I just wanted to indicate an alternative to using Alamofire only to make HTTP requests. The Alamofire is a cannon and I suggest using it only when necessary. That said, I leave here a code that can be pasted directly into a playground (Xcode 8) and GET to an endpoint of the Stack overflow api.

import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

let url = URL(string: "https://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflow")!
var request = URLRequest(url: url)
let session = URLSession(configuration: URLSessionConfiguration.default)

session.dataTask(with: request) { (data, response, error) in
    print("response:", response)
    print("error: ", error)

    var responseDictionary: NSDictionary?
    if data != nil {
        do {
            responseDictionary = try JSONSerialization.jsonObject(with: data!,
                                                 options: []) as? NSDictionary
            print(responseDictionary)
        } catch {
            print("error")
        }
    }


    }.resume()
  • You should give preference to using the native Swift3 dictionary [AnyHashable: Any] or in the case of a json dictionary [String: Any]

  • True. It’s a habit to occasionally use value(for: keypath) that is only in nsdictionary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.