2

I'm getting this error:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Most of the time (not all) when I'm using arc4random() in Xcode 6 with Swift. I didn't ever get this error in Xcode 5 in Objective C. Curiously, if I use arc4random_uniform(), I don't run into this problem.

How do I use arc4random() without getting this error?

aledalgrande
  • 5,167
  • 3
  • 37
  • 65
slooker
  • 153
  • 1
  • 1
  • 10
  • 2
    Remember it's still in beta. It might very well be a bug. Try to reproduce the problem in the very smallest piece you can and report it. – Fernando Mazzon Jun 04 '14 at 18:03
  • Restarting XCode 6 solved the problem for me when I faced the same issue. Looks like a bug. – Benzi Jun 05 '14 at 19:03
  • 1
    I thing it's simply an undefined behaviour in Apples internal code. You might solve this, by calling the C equivalent. – Leandros Jun 06 '14 at 10:31
  • Interesting. I tried restarting a few times and it didn't seem to change anything for me. – slooker Jun 06 '14 at 21:08
  • This same error is happening for me also. Nothing wrong on my part, just a bug on Apples. – Justin Cabral Jun 06 '14 at 22:11
  • Are you sure the problem is in `arc4random`? In this [question](http://stackoverflow.com/questions/24089631/how-to-pull-random-item-out-of-an-array-in-swift/24091085?noredirect=1#comment37157292_24091085) the problem was actually an out of bounds array index caused by `arc4random` returning negative numbers (which would be fixed with `arc4random_uniform`) – David Berry Jun 06 '14 at 22:32
  • 5
    The other possibility is that `arc4random` returns a `UInt32`, that means that half the time it's going to overflow an `Int32` if you try to assign by casting, since swift protects against overflows, that could cause crashes as well. `EXC_BAD_INSTRUCTION` is usually just an `abort()` call. Continue and check the log for more information. – David Berry Jun 06 '14 at 22:35

1 Answers1

0

You have to use it:

var _shiom = Int(arc4random_uniform(UInt32(arrayshiftingTile.count)))
  • Please explain what is worng with OP's code and why this solves the problem *by editing your answer*. – Baum mit Augen Sep 05 '14 at 13:20
  • func arc4random() -> UInt32 only return type but func arc4random_uniform(_: UInt32) -> UInt32 , Value set and return type –  Sep 05 '14 at 13:27