How can I take array values ​​with links?

advertisements

I am new in swift. Who can help? I have array with 3 links how can i take one link from this array and show in UIImageView then second link and then third? I make with one link but not understand how i can make with array. Thnx.

class ViewController: UIViewController {

    @IBOutlet weak var imgView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var image = UIImage()
        var imageData: Data?
        let links = ["http://s.ill.in.ua/i/gallery/950x0/2694/146192.jpg","http://s.ill.in.ua/i/gallery/950x0/2694/146190.jpg","http://s.ill.in.ua/i/gallery/950x0/2694/146202.jpg"]
        let url = URL(string: links[0])
        do {
            imageData = try Data(contentsOf: url!)
        }
        catch {
            print("error")
        }
        if let value = imageData {
            image = UIImage(data:value)!
            imgView.image = image
        }
    }

}


You can easily iterate through array like this:

for link in links {
    // This will iterate through array, so you can do something specific on each step
    let url = URL(string: link)
    do {
        imageData = try Data(contentsOf: url!)
    }
    catch {
        print("error")
    }
    if let value = imageData {
        image = UIImage(data:value)!
        imgView.image = image
        print("Updated imageView with \(link)!")
    }
}