NSTimer阵列?返回超出范围

时间:2015-11-10 07:38:48

标签: ios swift

var multiTimer: [NSTimer] = []

我尝试过使用

multiTimer[indexPath.row] = NSTimer.scheduledTimerWithTimeInterval(1,
            target: self, selector: "TargetFire:", userInfo: nil, repeats: true)

它返回超出范围的数组...

2 个答案:

答案 0 :(得分:2)

您必须将NSTimer附加到multiTimer

multiTimer.append(NSTimer.scheduledTimerWithTimeInterval(1,
        target: self, selector: "TargetFire:", userInfo: nil, repeats: true))

答案 1 :(得分:0)

大概你所追求的是一个包含可选定时器的数组,预先填充nil,以便以后可以按需添加定时器:

var multiTimer: [NSTimer?] = []

// add a loop to initialise your array with nil entries for each index path (pseudo code)

For i, multiTimer[i] = nil

// now you can replace the nil at any index with an initialised timer
multiTimer[indexPath.row] = NSTimer.scheduledTimerWithTimeInterval(1,
        target: self, selector: "TargetFire:", userInfo: nil, repeats: true)