0
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
    if error != nil {
        println("error=\(error)")
        return
    }

In this code I'm getting an error for dataTaskWithRequest(request) for with my PHP connection. It worked before migrating to Swift 2.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

Here is correct syntax:

let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {(data, response, error) in

    // your code

})

Sample code from this answer:

var url : NSURL = NSURL(string: "https://itunes.apple.com/search?term=\(searchTerm)&media=software")
var request: NSURLRequest = NSURLRequest(URL: url)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

    // notice that I can omit the types of data, response and error

    // your code

});

// do whatever you need with the task

UPDATE:

let url : NSURL = NSURL(string: "https://itunes.apple.com/search?term=&media=software")!
    let request: NSURLRequest = NSURLRequest(URL: url)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

        do {
            let JSON = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions(rawValue: 0))
            guard let JSONDictionary :NSDictionary = JSON as? NSDictionary else {
                print("Not a Dictionary")
                //get your JSONData here from dictionary.

                return
            }
            print("JSONDictionary! \(JSONDictionary)")
        }
        catch let JSONError as NSError {
            print("\(JSONError)")
        }

    })
Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165