I am working on my first project where I am trying to create a random color to a UILabel (when a button is pressed) that does not repeat itself twice in a row. I have read many questions and found codes that generates random numbers that does not appear twice in a row, however, when I "link" that random number to a UIColor and "link" that UIColor to a label the code no longer works. This code has no errors but it allows the same color to appear twice;
@IBOutlet var HelloWorld: UILabel!
@IBOutlet var MyButton: UIButton!
@IBAction func MyButton(sender: AnyObject) {
let randomNumber2 = Int(arc4random_uniform(5))
if randomNumber2 == 0 {
HelloWorld.textColor = UIColor.redColor()
}
else if randomNumber2 == 1 {
HelloWorld.textColor = UIColor.blueColor()
}
else if randomNumber2 == 2 {
HelloWorld.textColor = UIColor.yellowColor()
}
else if randomNumber2 == 3 {
HelloWorld.textColor = UIColor.orangeColor()
}
else if randomNumber2 == 4 {
HelloWorld.textColor = UIColor.blueColor()
}
Here is a code I have tried to copy from another answer but it has errors;
var previousNumber: UInt32?
@IBAction func MyButton(sender: AnyObject) {
func randomNumber() -> UInt32 {
var randomNumber = arc4random_uniform(5)
while previousNumber == randomNumber {
randomNumber = arc4random_uniform(5)
}
previousNumber = randomNumber
return randomNumber
}
if randomNumber2 == 0 {
HelloWorld.textColor = UIColor.redColor()
}
else if randomNumber2 == 1 {
HelloWorld.textColor = UIColor.blueColor()
}
else if randomNumber2 == 2 {
HelloWorld.textColor = UIColor.yellowColor()
}
else if randomNumber2 == 3 {
HelloWorld.textColor = UIColor.orangeColor()
}
else if randomNumber2 == 4 {
HelloWorld.textColor = UIColor.blueColor()
}
}
The error says: Binary operator '==' cannot be applied to operands of type '() -> UInt32' and 'Int'
Aucun commentaire:
Enregistrer un commentaire