I want use a method defined in an extension to UIImageView
in my ViewController. When the extension is in the same file as the view controller it works, however - I want to move it to it's own separate/public file, UIImageView+Download.swift
. When I do this however, I get an error in my view controller saying
Value of type 'UIImageView' has no member 'downloadFrom'
Here's the extension:
import Foundation
import UIKit
public extension UIImageView {
public func downloadFrom(urlString: String) {
guard let url = URL(string: urlString) else { return }
downloadFrom(url: url)
}
public func downloadFrom(url: URL) {
contentMode = .scaleAspectFit
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async {
self.image = UIImage(data: data)
}
}.resume()
}
}
And it's usage in the view controller:
if let itemImage = itemCategory?.items[indexPath.row].mainImage {
cell.itemImageView.downloadFrom(urlString: itemImage)
}