Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No exact matchec incall to instance method 'dataTask'

Hello i have a CollectionViewCell file, where i am trying to call public func configure cell.

Here is func

 public func configureCell(with cellViewModel: CellViewModel) {
        self.articleTitleLabel.text = cellViewModel.title
        
        if let data = cellViewModel.imageData {
            self.articleImage.image = UIImage(data: data)
        } else if let url = cellViewModel.urlToImage {
            URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
                guard let data = data && error == nil else { return }
                cellViewModel.imageData = data
                DispatchQueue.main.async {
                    self.articleImage.image = UIImage(data: data)
                }
            }
        }
    }

here is model

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct CellViewModel {
    
    let title: String
    let urlToImage: String?
    let imageData: Data? = nil
    
    init(title: String, urlToImage: String) {
        self.title = title
        self.urlToImage = urlToImage
    }
    
}

But i got error:

No exact matches in call to instance method ‘dataTask’

Why? How can i fix my code?

>Solution :

urlToImage is of type String but the datatask needs an argument of type URL.

You can use:

else if let stringurl = cellViewModel.urlToImage, let url = URL(string: stringurl){
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading