我希望只显示一次随机数组中的单词,然后在按下它后再显示一个随机单词。我正在探索实现它的不同方法,而且我还是Swift的新手。
@IBAction func ShowWord(_ sender: Any) {
struct ArrayRandom
{
var items : [String]
var seen = 0
init(_ items:[String])
{ self.items = items }
mutating func next() -> String
{
let index = Int(arc4random_uniform(UInt32(items.count - seen)))
let item = items.remove(at:index)
items.append(item)
seen = (seen + 1) % items.count
return item
}
}
var itemsarray = ArrayRandom (["Black", "Gold", "Red", "Blue", "Yellow", "Green", "Pink", "Orange", "Silver", "Bronze"])
func buttonPressed() // <- use your function here
{
print( itemsarray.next() )
}
randomWord?.text = itemsarray.next()
是否没有按预期方式工作?运行它时,我在数组中看到重复项。