基本上,我有一个数组,我希望在按下按钮时将其转移到标签。我遇到的主要问题是,我无法找到一种方法,让阵列在打印完成后不会重复某些内容
这是我的代码片段
@IBOutlet weak var truthspot: UILabel!
@IBAction func truthclick(_ sender: Any) {
let array = ["pasta", "boop", "test1", "test2"]
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
truthspot.text = (array[randomIndex])
答案 0 :(得分:0)
您应该将array
移到函数之外,并在每次调用函数时删除生成的索引处的元素。
var array = ["pasta", "boop", "test1", "test2"]
@IBAction func truthclick(_ sender: Any) {
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
truthspot.text = (array[randomIndex])
array.remove(at: randomIndex)
}