Reputation: 11
I'm trying to make a random number generator ios app in xcode and I'm getting an error while trying to use the rand() method I get the following error; "int32 is not convertible to int" I've tried to change "int" to "int32" and it gave me no errors but since I did that no numbers are displayed. How do I get around this problem?
Note: I'm using swift language
Code:
@IBOutlet var lblNum: UILabel!
@IBAction func btnGenerateNum(sender: UIButton, forEvent event: UIEvent) {
let text: Int = rand()
switch(text) {
case 0:
lblNum.text = "0"
break;
case 1:
lblNum.text = "1"
break;
case 2:
lblNum.text = "2"
break;
case 3:
lblNum.text = "3"
break;
case 4:
lblNum.text = "4"
break;
case 5:
lblNum.text = "5"
break;
case 6:
lblNum.text = "6"
break;
case 7:
lblNum.text = "7"
break;
case 8:
lblNum.text = "8"
break;
case 9:
lblNum.text = "9"
break;
default:
break;
}
Upvotes: 0
Views: 231
Reputation: 2938
Would this be easier for you? (Not necessarily a solution to the random number generator issue that you raised)
let text = String((Int(rand())) % 10)
lblNum.text = text
Upvotes: 0
Reputation: 236568
@IBAction func btnGenerateNum(sender: UIButton, forEvent event: UIEvent) {
lblNum.text = "\(arc4random_uniform(10))"
}
Upvotes: 1
Reputation: 37300
Simply initialize an Int with your Int32:
let text: Int = Int(rand())
Upvotes: 1