Home > Software engineering >  No exact matchec incall to instance method 'dataTask'
No exact matchec incall to instance method 'dataTask'

Time:06-14

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

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?

CodePudding user response:

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){

CodePudding user response:

The error you quote mentions an instance method DataTask (With a capital "D".) the method signature is dataTask(with:completionHandler:) (lower-case "d".)

The error doesn't match the code you posted.

  • Related