Paul Heinemeyer Paul Heinemeyer - 1 year ago 202
Swift Question

Swift 3 .dataTask error

After converting to Swift 3 I try to fix all the errors at the moment.
But this error makes me sick:

Cannot invoke 'dataTask' with an argument list of type '(with: NSMutableURLRequest, completionHandler: (Data?, URLResponse?, NSError?) -> Void)'

At this line of code (Its at the last line there) :

let request = NSMutableURLRequest(url: myUrl!)
request.httpMethod = "POST"
let para = ["userId" : user_ID]
let boundary = generateBoundaryString()
let imageA = images[pos]! as UIImage

request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

let imageData = UIImageJPEGRepresentation(imageA, 1)

request.httpBody = createBodyWithParameters(para, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)

URLSession.shared.dataTask(with:request, completionHandler: { (data:NSData, response:URLResponse?, error:NSError?) -> Void in

The code continues here, but thats not important for the solution.

Answer Source

The compiler wants

var request = URLRequest(url: myUrl!)


...(data: Data?, response:URLResponse?, error:Error?)

but I recommend to omit the type annotations completely

...(data, response, error)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download