gpt4 book ai didi

ios - 跟踪随机数

转载 作者:行者123 更新时间:2023-11-28 09:44:57 24 4
gpt4 key购买 nike

我想将图像应用到总共 12 个 UIButton 中的 3 个随机 UIButton。

编辑

这是工作代码;

let buttons:NSArray = [button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12].shuffle()

buttons[0].setImage(UIImage(named: "code1"), forState: .Normal)
buttons[1].setImage(UIImage(named: "code1"), forState: .Normal)
buttons[2].setImage(UIImage(named: "code1"), forState: .Normal)

和工作洗牌功能;

extension CollectionType {
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}

extension MutableCollectionType where Index == Int {
mutating func shuffleInPlace() {

if count < 2 { return }

for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}

最佳答案

这里有一个不同的想法:将所有 12 个 UIButtons 放入一个数组中,打乱该数组并操作前三个元素:

var buttons = [button1, button2, ..., button12]
buttons.shuffled()

// Do whatever with buttons[0] - button[2]
buttons[0].setImage(UIImage(named: "..."), forState: .Normal)
buttons[1].setImage(UIImage(named: "..."), forState: .Normal)
buttons[2].setImage(UIImage(named: "..."), forState: .Normal)

关于ios - 跟踪随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com