0

I get the error "extra argument" error "in call" my code is:

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
Rob
  • 415,655
  • 72
  • 787
  • 1,044
teko
  • 37
  • 8
  • In modern Swift, the Objective-C method with `NSError` parameter is replaced with a Swift method that throws errors, but takes no `NSError` parameter. E.g. `do { var urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response) } catch { print(error) }`. – Rob May 25 '16 at 22:51
  • 1
    If you search Stack Overflow for "extra argument error", you'll see many other questions on this topic. E.g. http://stackoverflow.com/q/33470527/1271826 is for a different API call, but exact same error caused by not using Swift [error handling](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508). – Rob May 25 '16 at 22:54

1 Answers1

0

Swift no longer uses error variables passed in as a parameter any more. Use a do/catch block instead:

var urlData: NSData?
let request = NSURLRequest() // Presumably declared already
var response: NSURLResponse? // Presumably declared already

do {
    urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
}
catch let error as NSError {
    print("Error: \(error.localizedDescription)")
}

Note also that sendSynchronousRequest is deprecated, and you should likely change it to dataTaskWithRequest(request: NSURLRequest) -> NSURLSessionDataTask instead.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67