Matthew Xavier Jacome Matthew Xavier Jacome - 2 months ago 23
Swift Question

Why do I get ambiguous reference to member error?

Using the following code

if let url = NSURL(string: "https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Full_Sail_University.svg/1280px-Full_Sail_University.svg.png") {

let task = session.dataTask(with: url, completionHandler: { (data, response, error) in
// check against error
if let error = error {
print("Data task failed with error: \(error)")
return
}

print("Success")

})
task.resume()
}


I get the following
Error:


Ambiguous reference to member 'dataTask(with:completionHandler:)'


Why and how can I fix this?

Answer Source

Error is in the NSURL. Change the

if let url = NSURL(string: "https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Full_Sail_University.svg/1280px-Full_Sail_University.svg.png")  

to

if let url = URL(string: "https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Full_Sail_University.svg/1280px-Full_Sail_University.svg.png") {