我试图在xcode中创建一个随机数生成器ios应用程序,并且在尝试使用rand()方法时遇到错误我收到以下错误; " int32不能转换为int"我试图改变" int" to" int32"它没有给我任何错误但是因为我这样做没有显示任何数字。我该如何解决这个问题?
注意:我使用的是快速语言
代码:
@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;
}
答案 0 :(得分:1)
只需使用Int32初始化Int:
let text: Int = Int(rand())
答案 1 :(得分:1)
return render_template("error.html", usa=usa, canada=canada, breadcrumbs=breadcrumbs, error_code=e), 404
答案 2 :(得分:0)
这对你来说会更容易吗? (不一定是您提出的随机数生成器问题的解决方案)
let text = String((Int(rand())) % 10)
lblNum.text = text