在 iOS 中消除带有 Swift 的 switch 语句中的重复
Eliminating repeats in a switch statement w/ Swift in iOS
在我正在构建的这个小型练习应用程序中,背景应该每 .25 秒保持交替颜色,因为它看起来真的很酷。但是由于我使用的是 "arc4random_uniform" 函数,它偶尔会连续两次选择相同的情况,导致我不喜欢相同颜色的持续时间很长。关于如何消除这些 switch 语句中的立即重复以便它确实连续两次选择相同的情况有什么想法吗?
switch arc4random_uniform(10) {
case 0:
self.backgroundImage.backgroundColor = UIColor.redColor()
case 1:
self.backgroundImage.backgroundColor = UIColor.orangeColor()
case 2:
self.backgroundImage.backgroundColor = UIColor.yellowColor()
case 3:
self.backgroundImage.backgroundColor = UIColor.greenColor()
case 4:
self.backgroundImage.backgroundColor = UIColor.blueColor()
case 5:
self.backgroundImage.backgroundColor = UIColor.purpleColor()
case 6:
self.backgroundImage.backgroundColor = UIColor.blackColor()
case 7:
self.backgroundImage.backgroundColor = UIColor.whiteColor()
case 8:
self.backgroundImage.backgroundColor = UIColor.brownColor()
case 9:
self.backgroundImage.backgroundColor = UIColor.grayColor()
case 10:
self.backgroundImage.backgroundColor = UIColor.blackColor()
default:
self.backgroundImage.backgroundColor = UIColor.clearColor()
break;
}
尝试用以前的值存储一个变量,在你输入 switch 语句之前有一个 while
语句你做
var num = arc4random_uniform(10);
while(num == prev) {
num = arc4random_uniform(10);
}
然后进入你的 switch 语句。
Eli 的答案可行,但在 while 循环中重新生成随机数效率低得不必要。相反,如果当前随机数与前一个相同,我建议使用除以 10 时的余数递增随机数,并在 switch 语句中使用该数字,这样就不会不必要地重复随机操作,例如:
var prevNum:UInt32 = 0
func changeColor() {
var randomNum = arc4random_uniform(10)
if randomNum == prevNum {
randomNum = (randomNum+1)%10
}
prevNum = randomNum
// insert your switch statement here
}
理论上(尽管在实践中不太可能)Eli 的代码会导致无限循环。
您可以通过创建数组然后将其打乱来创建颜色循环。这样,数字就不能重复了。然后你可以遍历数组并使用每个值一次。
var numbers = [Int](0 ..< 10)
for i in 0 ..< 10
{
let j = Int(arc4random_uniform(10))
(numbers[i], numbers[j]) = (numbers[j], numbers[i])
}
为避免生成新数组时重复,可以使用
if numbers.first == oldNumbers.last
{
numbers.remove(at: 0)
}
在我正在构建的这个小型练习应用程序中,背景应该每 .25 秒保持交替颜色,因为它看起来真的很酷。但是由于我使用的是 "arc4random_uniform" 函数,它偶尔会连续两次选择相同的情况,导致我不喜欢相同颜色的持续时间很长。关于如何消除这些 switch 语句中的立即重复以便它确实连续两次选择相同的情况有什么想法吗?
switch arc4random_uniform(10) {
case 0:
self.backgroundImage.backgroundColor = UIColor.redColor()
case 1:
self.backgroundImage.backgroundColor = UIColor.orangeColor()
case 2:
self.backgroundImage.backgroundColor = UIColor.yellowColor()
case 3:
self.backgroundImage.backgroundColor = UIColor.greenColor()
case 4:
self.backgroundImage.backgroundColor = UIColor.blueColor()
case 5:
self.backgroundImage.backgroundColor = UIColor.purpleColor()
case 6:
self.backgroundImage.backgroundColor = UIColor.blackColor()
case 7:
self.backgroundImage.backgroundColor = UIColor.whiteColor()
case 8:
self.backgroundImage.backgroundColor = UIColor.brownColor()
case 9:
self.backgroundImage.backgroundColor = UIColor.grayColor()
case 10:
self.backgroundImage.backgroundColor = UIColor.blackColor()
default:
self.backgroundImage.backgroundColor = UIColor.clearColor()
break;
}
尝试用以前的值存储一个变量,在你输入 switch 语句之前有一个 while
语句你做
var num = arc4random_uniform(10);
while(num == prev) {
num = arc4random_uniform(10);
}
然后进入你的 switch 语句。
Eli 的答案可行,但在 while 循环中重新生成随机数效率低得不必要。相反,如果当前随机数与前一个相同,我建议使用除以 10 时的余数递增随机数,并在 switch 语句中使用该数字,这样就不会不必要地重复随机操作,例如:
var prevNum:UInt32 = 0
func changeColor() {
var randomNum = arc4random_uniform(10)
if randomNum == prevNum {
randomNum = (randomNum+1)%10
}
prevNum = randomNum
// insert your switch statement here
}
理论上(尽管在实践中不太可能)Eli 的代码会导致无限循环。
您可以通过创建数组然后将其打乱来创建颜色循环。这样,数字就不能重复了。然后你可以遍历数组并使用每个值一次。
var numbers = [Int](0 ..< 10)
for i in 0 ..< 10
{
let j = Int(arc4random_uniform(10))
(numbers[i], numbers[j]) = (numbers[j], numbers[i])
}
为避免生成新数组时重复,可以使用
if numbers.first == oldNumbers.last
{
numbers.remove(at: 0)
}