为什么我的 if 语句代码不是 运行 in swift?
Why is my if statement code not running in swift?
import UIKit
class ViewController: UIViewController {
var timerCount = 5
var timerRunning = false
var timer = NSTimer()
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
let c = count(list)
for i in 0..<(c - 1) {
let j = Int(arc4random_uniform(UInt32(c - i))) + i
swap(&list[i], &list[j])
}
return list
}
func counting(){
timerCount -= 1
timerLabel.text = "\(timerCount)"
}
@IBOutlet weak var alphebetLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var sign1: UIImageView!
@IBOutlet weak var sign2: UIImageView!
@IBOutlet weak var sign3: UIImageView!
@IBOutlet weak var sign4: UIImageView!
@IBOutlet weak var sign5: UIImageView!
@IBOutlet weak var color1: UIImageView!
@IBOutlet weak var color2: UIImageView!
@IBOutlet weak var color3: UIImageView!
@IBOutlet weak var color4: UIImageView!
@IBOutlet weak var color5: UIImageView!
@IBOutlet weak var blueButton: UIButton!
@IBOutlet weak var purpleButton: UIButton!
@IBOutlet weak var orangeButton: UIButton!
@IBOutlet weak var yellowButton: UIButton!
@IBOutlet weak var redButton: UIButton!
override func viewDidLoad() {
var gameViewStillRunning = true
var sign1RandNum = arc4random_uniform(3) + 1
var sign2RandNum = arc4random_uniform(4) + 1
var sign3RandNum = arc4random_uniform(4) + 1
var sign4RandNum = arc4random_uniform(4) + 1
var sign5RandNum = arc4random_uniform(4) + 1
var colorArray = ["orangeBrick", "blueBrick", "yellowBrick", "redBrick", "purpleBrick"]
var shuffledColorArray: Array = shuffle(colorArray)
self.color1.image = UIImage(named: shuffledColorArray[0])
self.color2.image = UIImage(named: shuffledColorArray[1])
self.color3.image = UIImage(named: shuffledColorArray[2])
self.color4.image = UIImage(named: shuffledColorArray[3])
self.color5.image = UIImage(named: shuffledColorArray[4])
var correctAnswer = 0
switch sign1RandNum{
case 1:
self.sign1.image = UIImage(named: "blank")
case 2:
self.sign1.image = UIImage(named: "forward")
case 3:
self.sign1.image = UIImage(named: "fastForward")
case 4:
self.sign1.image = UIImage(named: "backSpace")
default:
break
}
switch sign2RandNum{
case 1:
self.sign2.image = UIImage(named: "blank")
case 2:
self.sign2.image = UIImage(named: "forward")
case 3:
self.sign2.image = UIImage(named: "fastForward")
case 4:
self.sign2.image = UIImage(named: "backSpace")
default:
break
}
switch sign3RandNum{
case 1:
self.sign3.image = UIImage(named: "blank")
case 2:
self.sign3.image = UIImage(named: "forward")
case 3:
self.sign3.image = UIImage(named: "fastForward")
case 4:
self.sign3.image = UIImage(named: "backSpace")
default:
break
}
switch sign4RandNum{
case 1:
self.sign4.image = UIImage(named: "blank")
case 2:
self.sign4.image = UIImage(named: "forward")
case 3:
self.sign4.image = UIImage(named: "fastForward")
case 4:
self.sign4.image = UIImage(named: "backSpace")
default:
break
}
switch sign5RandNum{
case 1:
self.sign5.image = UIImage(named: "blank")
case 2:
self.sign5.image = UIImage(named: "forward")
case 3:
self.sign5.image = UIImage(named: "fastForward")
case 4:
self.sign5.image = UIImage(named: "backSpace")
default:
break
}
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)
timerRunning = true
}
if timerLabel.text == "0"{
self.alphebetLabel.text = "B"
}
}
}
尽管所有这些代码 运行 都很好并且项目已构建,但末尾更改测试人员标签文本的 if 语句未 运行。一旦倒计时达到 0,标题根本不会改变。我还尝试将其他功能植入该 if 语句中,但代码永远不会到达。有什么建议吗?
将 if 语句移到计数函数的末尾,它似乎会执行您想要的操作。
import UIKit
class ViewController: UIViewController {
var timerCount = 5
var timerRunning = false
var timer = NSTimer()
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
let c = count(list)
for i in 0..<(c - 1) {
let j = Int(arc4random_uniform(UInt32(c - i))) + i
swap(&list[i], &list[j])
}
return list
}
func counting(){
timerCount -= 1
timerLabel.text = "\(timerCount)"
}
@IBOutlet weak var alphebetLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var sign1: UIImageView!
@IBOutlet weak var sign2: UIImageView!
@IBOutlet weak var sign3: UIImageView!
@IBOutlet weak var sign4: UIImageView!
@IBOutlet weak var sign5: UIImageView!
@IBOutlet weak var color1: UIImageView!
@IBOutlet weak var color2: UIImageView!
@IBOutlet weak var color3: UIImageView!
@IBOutlet weak var color4: UIImageView!
@IBOutlet weak var color5: UIImageView!
@IBOutlet weak var blueButton: UIButton!
@IBOutlet weak var purpleButton: UIButton!
@IBOutlet weak var orangeButton: UIButton!
@IBOutlet weak var yellowButton: UIButton!
@IBOutlet weak var redButton: UIButton!
override func viewDidLoad() {
var gameViewStillRunning = true
var sign1RandNum = arc4random_uniform(3) + 1
var sign2RandNum = arc4random_uniform(4) + 1
var sign3RandNum = arc4random_uniform(4) + 1
var sign4RandNum = arc4random_uniform(4) + 1
var sign5RandNum = arc4random_uniform(4) + 1
var colorArray = ["orangeBrick", "blueBrick", "yellowBrick", "redBrick", "purpleBrick"]
var shuffledColorArray: Array = shuffle(colorArray)
self.color1.image = UIImage(named: shuffledColorArray[0])
self.color2.image = UIImage(named: shuffledColorArray[1])
self.color3.image = UIImage(named: shuffledColorArray[2])
self.color4.image = UIImage(named: shuffledColorArray[3])
self.color5.image = UIImage(named: shuffledColorArray[4])
var correctAnswer = 0
switch sign1RandNum{
case 1:
self.sign1.image = UIImage(named: "blank")
case 2:
self.sign1.image = UIImage(named: "forward")
case 3:
self.sign1.image = UIImage(named: "fastForward")
case 4:
self.sign1.image = UIImage(named: "backSpace")
default:
break
}
switch sign2RandNum{
case 1:
self.sign2.image = UIImage(named: "blank")
case 2:
self.sign2.image = UIImage(named: "forward")
case 3:
self.sign2.image = UIImage(named: "fastForward")
case 4:
self.sign2.image = UIImage(named: "backSpace")
default:
break
}
switch sign3RandNum{
case 1:
self.sign3.image = UIImage(named: "blank")
case 2:
self.sign3.image = UIImage(named: "forward")
case 3:
self.sign3.image = UIImage(named: "fastForward")
case 4:
self.sign3.image = UIImage(named: "backSpace")
default:
break
}
switch sign4RandNum{
case 1:
self.sign4.image = UIImage(named: "blank")
case 2:
self.sign4.image = UIImage(named: "forward")
case 3:
self.sign4.image = UIImage(named: "fastForward")
case 4:
self.sign4.image = UIImage(named: "backSpace")
default:
break
}
switch sign5RandNum{
case 1:
self.sign5.image = UIImage(named: "blank")
case 2:
self.sign5.image = UIImage(named: "forward")
case 3:
self.sign5.image = UIImage(named: "fastForward")
case 4:
self.sign5.image = UIImage(named: "backSpace")
default:
break
}
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)
timerRunning = true
}
if timerLabel.text == "0"{
self.alphebetLabel.text = "B"
}
}
}
尽管所有这些代码 运行 都很好并且项目已构建,但末尾更改测试人员标签文本的 if 语句未 运行。一旦倒计时达到 0,标题根本不会改变。我还尝试将其他功能植入该 if 语句中,但代码永远不会到达。有什么建议吗?
将 if 语句移到计数函数的末尾,它似乎会执行您想要的操作。