Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Request in Swift with NSURLSession

I am trying to make an HTTP Request in an iOS app with Swift using NSURLSession, but XCode throws an error. I've been searching all over stackoverflow and the web and my code is pretty similar to what people suggested, but for some reason it doesn't work. I am on XCode 6.0.1 (6A317).

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url),
    completionHandler : {(data, response, error) -> Void in
        println("Done")
})
session.resume()

The error points to the first line above: "Extra argument 'completionHandler' in call"

Using Trailing Closure throws a similar error:

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in
    println("Done")
}
session.resume()

The error is also on the first line: "Extra argument in call"

Did I miss something here?

like image 836
Irwan Avatar asked Jun 12 '26 15:06

Irwan


1 Answers

Simple get request would be like this:

let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "url_here")) { data, response, error in
}
task.resume()
like image 153
mustafa Avatar answered Jun 15 '26 06:06

mustafa