为什么我的if语句代码没有在swift中运行?

时间:2015-05-20 18:41:45

标签: ios swift if-statement nstimer

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语句但是从未达到过代码。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

将if语句移动到计数功能的结尾,看起来它会像你想要的那样。