-1

I am trying to download an image using the following code:

 if let imageUrl = post.postImageURL{
        let url = NSURL(string: imageUrl)
        URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
            if error != nil{
                print(error)
                return
            }
            DispatchQueue.main.async {
                cell.itemImage.image = UIImage(data: data)
            }

        }).resume()
    }

When I try to build it, I get the following error message:

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

How can I fix this error?

Thanks in advance!

rmaddy
  • 298,130
  • 40
  • 468
  • 517

1 Answers1

1

The API expects Swift URL struct, not Foundation NSURL class.

Just remove NS

let url = URL(string: imageUrl)

Basically don't use Foundation NS.. classes if there is a native Swift counterpart.

vadian
  • 232,468
  • 27
  • 273
  • 287