2

I created a list called imageNames. and i used thise chunk of code to get a random image

@IBAction func showImages(_ sender: Any) {

    let RandomImage: Int = Int
        (arc4random_uniform(20))
    imageOne.image = UIImage (named:
        imageNames[RandomImage])

for some reason it won't work for me. i thought about making a var instead og let but still it gives me an error at int = int

anyone care to assist?

Chris
  • 4,009
  • 3
  • 21
  • 52
Daniel Saggo
  • 108
  • 1
  • 9
  • Cannot convert value of type 'Int.Type' to specified type 'Int' – Daniel Saggo Aug 12 '18 at 20:01
  • 1
    solution is in the edit though =D – Shehata Gamal Aug 12 '18 at 20:25
  • 1
    Sh_Khan, don't edit the OPs original code. That hides the cause of the original error, and makes the whole thread very confusing to other people reading it. If you're going to provide a solution to the OPs problem, post an answer, but **don't** edit the original question to correct the code. – Duncan C Aug 12 '18 at 20:39

5 Answers5

3

Get rid of the newline between Int and the (. That's what's causing your error.

Also, you should use the size of the array of images names to control the range of your random index. (If you hard-code the range of numbers and then later change your array of image names you'll either crash if your have less names in the array, or silently never select any of the new names if you don't increase the max index.)

And while we're at it, variable names should start with a lower-case letter. Consider this code:

@IBAction func showImages(_ sender: Any) {
    //Sample image names - replace with your own array of names
    let imageNames = ["one", "two", "three"]

    let randomIndex: Int = Int(arc4random_uniform(UInt32(imageNames.count)))
    let randomName = imageNames[randomIndex]
    imageOne.image = UIImage(name: randomName)
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
3

Here’s an answer for Swift 4.2, where arc4random is not necessary.

@IBAction func showImages(_ sender: Any) {
    let randomIndex = Int.random(in: 0..<imageNames.count)
    imageOne.image = UIImage(named: imageNames[randomIndex])
Chris
  • 4,009
  • 3
  • 21
  • 52
  • 2
    The new radom() functions in Swift 4.2 will be cool, but you should point out that Swift 4.2 hasn't been released yet. (There are developer versions available but we can't yet use it for app store submissions, and I would not recommend using it for any production code including in-house apps.) – Duncan C Aug 12 '18 at 20:33
  • @DuncanC That’s a good point, thanks. Plus I guess the question title explicitly asks about `arc4random`. – Chris Aug 12 '18 at 20:35
  • 1
    If you edit your answer to point out that Swift 4.2 is not yet released I'll up-vote it since it points out a useful up-and-coming feature of the language. – Duncan C Aug 12 '18 at 20:37
  • 1
    `Int.random(in: imageNames.indices)` Btw make sure the array it is not empty `Int.random(in: 0..<0)` would crash your app – Leo Dabus Aug 12 '18 at 22:27
1

In Swift 4.2 it's still easier

imageOne.image = UIImage(named: imageNames.randomElement())
vadian
  • 274,689
  • 30
  • 353
  • 361
0

I think you need it one line

let RandomImage = Int(arc4random_uniform(20))

Also look here for Swift 4.2 random number generation

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

I prefer to use shuffle() method to reorder an array elements randomly.

Hendra Kusumah
  • 186
  • 3
  • 6