1

I am trying to fetch a random item from an array containing 3 strings as follows :

func selectRandomImage () {
        var imageName : String? //Optional

        var arrayCount : NSNumber = animalsArray.count //Bridges to an NSNumber

        var x : Int  = Int(arc4random())%(arrayCount.integerValue)

        var name : String  = animalsArray[x] as String

        println("Name is \(name)")

    }

However every 5 runs or so of the code, I am getting a crash with EXC_BAD_INSTRUCTION.

Could anyone advise on what could be wrong or how to troubleshoot ?

Thanks !

GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113

1 Answers1

0

arc4random doesent work well with swift, it crushes when you try to cast it to Int

Try Int(rand()) instead of arc4random()

Alexey Sidorov
  • 846
  • 1
  • 9
  • 17