top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to cast arc4random() to Int in SWIFT

+1 vote
1,830 views

How to convert arc4random() to Int , i tried as below but it throws error

 var items = Color[]()
 var i = Int(arc4random()) % items.count

and the colour is enum type

enum Color: Int {
    case Red, Green, Blue
}

The error is EXC_BAD_INSTRUCTION. Checking the debugger shows items is size=1 and [0] = (AppName.Color) (0x10).

posted Jul 23, 2014 by Shiva

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You should cast items.count into UInt32

Int(arc4random_uniform((UInt32(items.count)))
answer Aug 4, 2014 by Raju
Similar Questions
+1 vote

How to declare single parameter which accept multiple values.

+1 vote

How to use default values for parameter in function. What will happens when value is not passed for a default parameters.

0 votes

I am facing situation to use external parameter name to a function parameter name .. How to achieve this

0 votes

Is it possible to set multiple return type for function in SWIFT. If yes please provide me syntax .

0 votes

How to set return type for function in SWIFT.

...