Convert code on swift 3

Swift 3 changed many part of codes. I have problems to convert the below code to Swift 3. Somebody knows how to write on Swift 3


session.dataTaskWithURL(url! as URL, completionHandler: { (data : NSData?, response : URLResponse?, error : NSError?) -> Void in
              
                if error != nil {
                    callback(items: nil, errorDescription: error!.localizedDescription, placesDetail: [])
                }
              
                if let statusCode = response as? NSHTTPURLResponse {
                    if statusCode.statusCode != 200 {
                        callback(items: nil, errorDescription: "Could not continue.  HTTP Status Code was \(statusCode)", placesDetail: [])
                    }
                }
              
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    callback(items: GooglePlaces.parseFromData(data!), errorDescription: nil, placesDetail: GooglePlaces.arrayPlaces(data!))
                })
            }).resume()

Accepted Reply

Which part are you having problems with? Here’s a snippet of how to you’d write basic URLSession code in Swift 3.

session.dataTask(with: URL(string: "http://www.example.com")!) { (data, response, error) in
  if let error = error {
      print("error: \(error)")
  } else {
      let response = response as! HTTPURLResponse
      let data = data!
      print("status: \(response.statusCode)")
      for (key, value) in response.allHeaderFields {
          print("header: \(key) = \(value)")
      }
      print("body: \(data as NSData)")
  }
}.resume()

Your NSOperation code would look like this:

OperationQueue.main.addOperation {
    print("Woo hoo, I'm on the main thread!")
}

I can’t really help you with the

GooglePlaces
side of things.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Which part are you having problems with? Here’s a snippet of how to you’d write basic URLSession code in Swift 3.

session.dataTask(with: URL(string: "http://www.example.com")!) { (data, response, error) in
  if let error = error {
      print("error: \(error)")
  } else {
      let response = response as! HTTPURLResponse
      let data = data!
      print("status: \(response.statusCode)")
      for (key, value) in response.allHeaderFields {
          print("header: \(key) = \(value)")
      }
      print("body: \(data as NSData)")
  }
}.resume()

Your NSOperation code would look like this:

OperationQueue.main.addOperation {
    print("Woo hoo, I'm on the main thread!")
}

I can’t really help you with the

GooglePlaces
side of things.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"