Im stuck/stumped - Array of UIView In a structure

Im trying to have a swipe func showing an array of images using a "For In Loop". The images and data that I want presented separately in a UIView for the images and and a few UILabels for the car information. Im getting stuck of how to extract the images "name" and putting them in the "for in" loop. Nothing seems to be working.


import UIKit

struct ExoticCars {
  let name: UIImage;
  let make: String;
  let model: String;
  let year: Int;
  let topSpeed: Double;
  let price: Int;



class ViewController: UIViewController {

  @IBOutlet weak var imageVIew: UIImageView!
  @IBOutlet weak var makeLabel: UILabel!
  @IBOutlet weak var modelLabel: UILabel!
  @IBOutlet weak var yearLabel: UILabel!
  @IBOutlet weak var speedLabel: UILabel!
  @IBOutlet weak var priceLabel: UILabel!


  var index = 0

  var exoticCar: [ExoticCars] = [
  ExoticCars(name: imageLiteral(resourceName: "porsche_spyder"), make: "Porsche", model: "918 Spyder", year: 2015, topSpeed: 2.2, price: 931_975),
  ExoticCars(name:  imageLiteral(resourceName: "tesla_p"), make: "Tesla", model: "Model S P100D", year: 2019, topSpeed: 2.28, price: 121_190),
  ExoticCars(name:  imageLiteral(resourceName: "dodge_challenger"), make: "Dodge", model: "Challenger SRT", year: 2018, topSpeed: 2.3, price: 86_090),
  ExoticCars(name:  imageLiteral(resourceName: "ferrari_laferrari"), make: "Ferrari", model: "LaFerrari", year: 2019, topSpeed: 2.4, price: 2_200_000),
  ExoticCars(name:  imageLiteral(resourceName: "bugatti_chiron"), make: "Bugatti", model: "Chiron", year: 2019, topSpeed: 2.5, price: 3_000_000),
  ExoticCars(name:  imageLiteral(resourceName: "rimac_concept1"), make: "Rimac", model: "Concept One", year: 2018, topSpeed: 1.85, price: 1_200_000),
  ExoticCars(name:  imageLiteral(resourceName: "mclaren_720s"), make: "McLaren", model: "720s", year: 2019, topSpeed: 2.0, price: 299_000),
  ExoticCars(name:  imageLiteral(resourceName: "porsche_gt2"), make: "Porsche", model: "911 GT2 RS", year: 2018, topSpeed: 2.6, price: 294_450),
  ExoticCars(name:  imageLiteral(resourceName: "lamborghini_aventador"), make: "Lamborghini", model: "Aventador LP", year: 2016, topSpeed: 2.7, price: 497_895),
  ExoticCars(name:  imageLiteral(resourceName: "koenigsegg"), make: "Koenigsegg", model: "Regera", year: 2017, topSpeed: 2.8, price: 2_000_000),
  ];

  override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view.


  var name = [UIView]()

  for name in exoticCar {

  }


  }
  @IBAction func swipeLeft(_ sender: UISwipeGestureRecognizer) {



  if name < exoticCar.count - 1 {
  index = index + 1

  }

  }
  @IBAction func swipeRight(_ sender: UISwipeGestureRecognizer) {

  if index < exoticCar.count + 1 {
  index = index - 1
  }

  }
  }
}

Accepted Reply

It is not clear enough, as your shown code is broken and incomplete. But I cannot find any reason you need for-in to extract something.

(And your code is a little bit confusing, why an image property is named as "name"?)


You just need to use the property "name", when you want to access the image:

class ViewController: UIViewController {
    
    @IBOutlet weak var imageVIew: UIImageView!
    @IBOutlet weak var makeLabel: UILabel!
    @IBOutlet weak var modelLabel: UILabel!
    @IBOutlet weak var yearLabel: UILabel!
    @IBOutlet weak var speedLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
    var index = 0
    
    var exoticCar: [ExoticCars] = [
        //...
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func swipeLeft(_ sender: UISwipeGestureRecognizer) {
        if index < exoticCar.count - 1 {
            index = index + 1
            showCar(at: index)
        }
    }
    
    @IBAction func swipeRight(_ sender: UISwipeGestureRecognizer) {
        if index < exoticCar.count + 1 {
            index = index - 1
            showCar(at: index)
        }
    }
    
    func showCar(at index: Int) {
        let car = exoticCar[index]
        self.imageVIew.image = car.name
        self.makeLabel.text = car.make
        //...
    }
}


But you should better consider UIScrollView with paging or UICollectionView, to show multiple contents with swiping.

Replies

It is not clear enough, as your shown code is broken and incomplete. But I cannot find any reason you need for-in to extract something.

(And your code is a little bit confusing, why an image property is named as "name"?)


You just need to use the property "name", when you want to access the image:

class ViewController: UIViewController {
    
    @IBOutlet weak var imageVIew: UIImageView!
    @IBOutlet weak var makeLabel: UILabel!
    @IBOutlet weak var modelLabel: UILabel!
    @IBOutlet weak var yearLabel: UILabel!
    @IBOutlet weak var speedLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
    var index = 0
    
    var exoticCar: [ExoticCars] = [
        //...
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func swipeLeft(_ sender: UISwipeGestureRecognizer) {
        if index < exoticCar.count - 1 {
            index = index + 1
            showCar(at: index)
        }
    }
    
    @IBAction func swipeRight(_ sender: UISwipeGestureRecognizer) {
        if index < exoticCar.count + 1 {
            index = index - 1
            showCar(at: index)
        }
    }
    
    func showCar(at index: Int) {
        let car = exoticCar[index]
        self.imageVIew.image = car.name
        self.makeLabel.text = car.make
        //...
    }
}


But you should better consider UIScrollView with paging or UICollectionView, to show multiple contents with swiping.

if you just add line 57 for instance:

imageVIew.image = exoticCar[index].name


doesn't it work ?


Note: giving the name

var name : UIImage

is really misleading

Why didn't you give an explicit name, such as

var varImage : UIImage


That would certainly ease code reading.


Note 2: in Swift, you dont need to end lines with a semi colon.