@IBAction func buttonTapped() {
//I figured out how to put random numbers but I want to put random words that I write
var randomText = String(arc4random_uniform(5))
textLabel.setText(randomText)
}
基本上,当点击按钮时,不是显示随机数,而是希望显示随机字符串。例如["是","没有",可能"]随机出现而不是数字0-5。
我该怎么做?
答案 0 :(得分:0)
你应该创建一个包含你想要显示的所有字符串的数组,然后生成一个介于0和数组之间的随机数(索引减去1),然后使用该随机数从数组中获取一个字符串
答案 1 :(得分:0)
func getRandomString()->String{
let sArray = ["Yes","No","Maybe","Like","Ok"] //can have any number of words
let count = UInt32(sArray.count)
let randNumber = Int(arc4random_uniform(count))
return sArray[randNumber]
}
println(getRandomString())