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;
}
只需使用您的Int32初始化一个Int:
let text: Int = Int(rand())
@IBAction func btnGenerateNum(sender: UIButton, forEvent event: UIEvent) {
lblNum.text = "\(arc4random_uniform(10))"
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.