Paul Heinemeyer Paul Heinemeyer - 16 days ago 24x
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.


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)