我有一个函数,它随机选择一个UIButton
并将其字符设置为某个表情符号。现在我想将剩余的UIButtons
设置为随机表情符号。
我如何确定哪些UIButtons
没有从随机生成器设置一个值?
我还想确保被分配的值与在随机生成器中插入的值不同。
@IBOutlet weak var topLeftAnswer: UIButton!
@IBOutlet weak var topRightAnswer: UIButton!
@IBOutlet weak var bottomLeftAnswer: UIButton!
@IBOutlet weak var bottomRightAnswer: UIButton!
func correctAnswerGen() {
var correct: UInt32 = arc4random_uniform(4)
switch correct{
case 0: topLeftAnswer.setTitle("😄", forState: UIControlState.Normal)
case 1: topRightAnswer.setTitle("😄", forState: UIControlState.Normal)
case 2: bottomLeftAnswer.setTitle("😄", forState: UIControlState.Normal)
case 3: bottomRightAnswer.setTitle("😄", forState: UIControlState.Normal)
default: break
}
//assign other 3 buttons to another emoji value.
}
同样的道理:
func randomEmoji() -> String{
let emojies = [UInt32](0x1F601...0x1F64F)
+ [UInt32](0x2702...0x27B0)
+ [UInt32](0x1F680...0x1F6C0)
+ [UInt32](0x1F170...0x1F251)
let rand = Int(arc4random_uniform(UInt32(emojies.count - 1)))
return String(UnicodeScalar(emojies[rand]))
}
设置一个随机的表情符号,可以通过循环0x1F601...0x1F64F
,选择一个随机的表情符号,如:
var rand: UInt32 = arc4random_uniform(78)
for i in 0x1F601...0x1F64F {
if rand == i {
var c = String(UnicodeScalar(i))
print(c)
break
}
}
但是由于有更多的表情符号,你必须使用像这样的东西来循环遍历所有的表情符号:
let allEmojis = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
var rand: UInt32 = arc4random_uniform(544)
var counter = 0
for range in allEmojis {
for i in range {
if rand == counter
{
var c = String(UnicodeScalar(i))
print(c)
}
counter++
}
}