Cannot convert value of type 'Int' to expected argument type 'UInt32'

Tom Fox picture Tom Fox · Feb 22, 2016 · Viewed 26.4k times · Source

I am trying to generate a random number in Swift:

var amountOfQuestions = 2
var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1

but this results in the error:

Cannot convert value of type 'Int' to expected argument type 'UInt32'

What is the problem? Any ideas on what I can do to fix this error?

Answer

yesthisisjoe picture yesthisisjoe · Feb 22, 2016

Declare amountOfQuestions as a UInt32:

var amountOfQuestions: UInt32 = 2

PS: If you want to be grammatically correct it's number of questions.