2

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)
}
Brandon Lee
  • 194
  • 2
  • 12
  • 4
    Have you added the `UIImageView+Download.swift` to the [Target Membership](http://stackoverflow.com/a/14545754/224671) in Xcode? – kennytm Mar 09 '17 at 06:48
  • @kennytm Wow your reply was fast, and that worked! Not sure how it wasn't in target membership before... Thanks!!! – Brandon Lee Mar 09 '17 at 06:53

0 Answers0