Detect item already accessed in Array

0

I am working with Xcode and I need to place in different imageView a series of images that I have in an Array, with the condition that the images that previously have already been placed in an imageView, can not go back out .

The images are already randomly extracted from the Array using arc4random_uniform () and placed on the elements, but they are repeated, I want to avoid this.

Thanks in advance

    
asked by Wavid 13.12.2016 в 18:24
source

2 answers

0

To take the images and assign them without repeating you can do something like this:

var images = [UIImage(), UIImage(), UIImage(), UIImage(), UIImage()]

while images.count != 0 { // Ejecuta hasta que el array de imágenes está vacío

    let i = Int(arc4random_uniform(UInt32(images.count))) // Genera un aleatorio
    imageView.image = images[i] // asigna la imagen al imageView
    images.remove(at: i) // La elimina del array

}
    
answered by 13.12.2016 в 18:53
0

I created a struct called Images

Where you pass an array of UIImage

And then you can call your getRandomImageNotUsed method to have an unused image of the array returned

Once all the images have been used, calling getRandomImageNotUsed will return you nil.

Demo:

var image1 = UIImage()
var image2 = UIImage()
var image3 = UIImage()
var image4 = UIImage()

// le asigno nombres para proposito
// de demostrarte el demo.
image1.accessibilityIdentifier = "image1"
image2.accessibilityIdentifier = "image2"
image3.accessibilityIdentifier = "image3"
image4.accessibilityIdentifier = "image4"

var images = Images(images: [image1, image2, image3, image4])

var randomImage1 = images.getRandomImageNotUsed()
var randomImage2 = images.getRandomImageNotUsed()
var randomImage3 = images.getRandomImageNotUsed()
var randomImage4 = images.getRandomImageNotUsed()

print(randomImage1?.accessibilityIdentifier)
print(randomImage2?.accessibilityIdentifier)
print(randomImage3?.accessibilityIdentifier)
print(randomImage4?.accessibilityIdentifier)

var randomImage5 = images.getRandomImageNotUsed()
print(randomImage5)

Output of running twice the code above:

Optional("image1")
Optional("image3")
Optional("image4")
Optional("image2")
nil
Optional("image2")
Optional("image3")
Optional("image4")
Optional("image1")
nil

Source Code of Images struct:

import UIKit

struct Images {
  private var images: [UIImage]
  private var unusedImageIndexes: [Int]

  init(images: [UIImage]) {
    self.images = images
    self.unusedImageIndexes = Array(0...images.count - 1)
  }

  mutating func getRandomImageNotUsed() -> UIImage? {
    if unusedImageIndexes.count > 0 {
      let randomValue = Int(arc4random_uniform(UInt32(unusedImageIndexes.count)))
      let randomIndex = unusedImageIndexes[randomValue]

      unusedImageIndexes.remove(at: randomValue)

      return images[randomIndex]
    } else {
      return nil
    }
  }
}

I hope it serves you something in your problem, let me know if you need any clarification of my humble solution, Greetings partner!

    
answered by 05.02.2017 в 19:50