-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
  • 314,917
  • 42
  • 532
  • 579
James Ajjaja
  • 95
  • 1
  • 5

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
  • 274,689
  • 30
  • 353
  • 361