使用 NSTimer 时出现 SIGABRT 错误,Swift 2 Xcode 7

SIGABRT error when using NSTimer, Swift 2 Xcode 7

我有一个按钮闪烁的功能。该函数位于 for i loop 中,它位于 viewDidLoad 函数中。我还有一个 NSTimer 调用函数,每次重复闪烁不同的按钮。但是,当我 运行 时,我得到了 SIGABRT error。如果你要问的话,我已经确保我的按钮连接牢固,没有断开连接。

var computerChoices = [Int](count: 11, repeatedValue: 0)
var randomIndex = 0
var pcChoice = 0
var lit = [b0o,b1o,b2o,b3o,b4o,b5o,b6o,b7o,b8o]
var litIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()

    for i in 1...10{

        print(randomIndex)
        print(computerChoices)

        var buttonChoice = lit[randomIndex]

        randomIndex = Int(arc4random_uniform(UInt32(lit.count)))
        computerChoices[i] = randomIndex

        print("yoyoyo")

        func flashingButtons(){
            var one = computerChoices[pcChoice]
            lit[one].setImage(UIImage(named: "redb.png"), forState: UIControlState.Normal)
            pcChoice += 1

        }
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("flashingButtons"), userInfo: nil, repeats: true)
    }
}

从 viewDidLoad 中取出 flashingButtons 函数。定时器找功能,内嵌找不到。

override func viewDidLoad() {
    super.viewDidLoad()

    for i in 1...10{

        print(randomIndex)
        print(computerChoices)

        var buttonChoice = lit[randomIndex]

        randomIndex = Int(arc4random_uniform(UInt32(lit.count)))
        computerChoices[i] = randomIndex

        print("yoyoyo")

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("flashingButtons"), userInfo: nil, repeats: true)
    }
}

func flashingButtons(){
    var one = computerChoices[pcChoice]
    lit[one].setImage(UIImage(named: "redb.png"), forState: UIControlState.Normal)
    pcChoice += 1

}

你应该把 flashingButtons() 放在 viewDidLoad 的外面。并在其中确保 out of range 不会发生,因为 pcChoice 随着时间的推移而增加。

 func flashingButtons(){
    var one = computerChoices[pcChoice]
    lit[one].setImage(UIImage(named: "redb.png"), forState: UIControlState.Normal)

    if pcChoice >= 10 {
        pcChoice = 0
        return
    }
    pcChoice += 1

}