随机选择一个插座并设置其他插座
Randomly choosing one outlet and setting others
我有一个随机选择 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.
}
要设置随机表情符号,您可以循环 0x1F601...0x1F64F
,然后 select 随机表情符号,例如:
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++
}
}
同样的事情简化了:
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]))
}
我有一个随机选择 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.
}
要设置随机表情符号,您可以循环 0x1F601...0x1F64F
,然后 select 随机表情符号,例如:
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++
}
}
同样的事情简化了:
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]))
}