为什么这个NSTimer会崩溃?

时间:2016-03-07 01:15:10

标签: swift

当我运行此代码时,在它第一次运行NSTimer之前,它崩溃,输出:" libc ++ abi.dylib:以NSException类型的未捕获异常终止" 在控制台中。我已经考虑了几天了,并且无法找到解决方案,欢迎任何帮助,谢谢。

编辑:代码位于IBAction中,其目的是将集合视图中某些单元格的颜色更改为白色或黑色,具体取决于它在对应的整数中的值。数组simulationUniverse [universeCounter]。在我添加NSTimer之前它正在运行,但由于屏幕更新速度太快,我只能看到显示的最后一个模式,抱歉之前没有添加它。

CONSOLE OUTPUT: 2016-03-06 23:44:54.935 Digiverse [33095:2612091] - [Digiverse.SimulatorViewController paintUniverse]:无法识别的选择器发送到实例0x7f91c504c6c0 2016-03-06 23:44:54.942 Digiverse [33095:2612091] *由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [Digiverse.SimulatorViewController paintUniverse]:无法识别选择器发送到实例0x7f91c504c6c0' * 第一次抛出调用堆栈: (     0 CoreFoundation 0x0000000105f92e65 exceptionPreprocess + 165     1 libobjc.A.dylib 0x0000000107cd2deb objc_exception_throw + 48     2 CoreFoundation 0x0000000105f9b48d - [NSObject(NSObject)doesNotRecognizeSelector:] + 205     3 CoreFoundation 0x0000000105ee890a ___转发_ + 970     4 CoreFoundation 0x0000000105ee84b8 _CF_forwarding_prep_0 + 120     5基金会0x00000001063780d1 NSFireTimer + 83     6 CoreFoundation 0x0000000105ef2c84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20     7 CoreFoundation 0x0000000105ef2831 __CFRunLoopDoTimer + 1089     8 CoreFoundation 0x0000000105eb4241 __CFRunLoopRun + 1937     9 CoreFoundation 0x0000000105eb3828 CFRunLoopRunSpecific + 488     10 GraphicsServices 0x000000010a5a8ad2 GSEventRunModal + 161     11 UIKit 0x00000001067af610 UIApplicationMain + 171     12 Digiverse 0x0000000105da524d main + 109     13 libdyld.dylib 0x00000001087db92d start + 1     14 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

let simAlgorithm = UniverseSimulationAlgorithm()

func checkPopPlaces ( strPlaces: [String]? ) -> [Int]? {

    var populatedPlaces: [Int]?

    if strPlaces! == [] {

        if size < 7 {
            populatedPlaces = nil
        }
        else {
            populatedPlaces = []
        }

    }
    else {

        populatedPlaces = []

        for str in populatedStringPlaces! {
            populatedPlaces!.append(Int(str)!)
        }

    }

    return populatedPlaces

}

var simulatedUniverse: [[Int]] {

    return simAlgorithm.simulateUniverse( size, populatedPlaces: checkPopPlaces(populatedStringPlaces))

}

var universeCounter = 0

func paintUniverse ( universeArray: [[Int]], var uniCounter: Int, trueSize: Int ) {

    var placeCounter = 0

    if uniCounter < universeArray.count {

        for place in universeArray[uniCounter] {

            if place == 1 {
                self.realCollectionView.cellForItemAtIndexPath(returnTheRightCell(placeCounter, rightSize: trueSize ))?.contentView.backgroundColor = UIColor.whiteColor()
            }
            else {
                self.realCollectionView.cellForItemAtIndexPath(returnTheRightCell(placeCounter, rightSize: trueSize ))?.contentView.backgroundColor = UIColor.blackColor()
            }

            placeCounter++

        }

        uniCounter++
    }
    else {
        timer.invalidate()
    }

}

@IBAction func startButtonPressed(sender: AnyObject) {

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

}

1 个答案:

答案 0 :(得分:1)

崩溃日志中最重要的信息是

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [Digiverse.SimulatorViewController paintUniverse]:无法识别的选择器发送到实例0x7f91c504c6c0'

计时器声明行中的选择器paintUniverse期望在类的顶层没有参数的方法func paintUniverse()

您的函数paintUniverse有三个参数,无论如何都不能用作计时器选择器。

计时器选择器根本没有参数:

selector: "paintUniverse" - &gt; func paintUniverse()

或一个必须是NSTimer实例的单个参数,而selector必须有一个尾随冒号:

selector: "paintUniverse:" - &gt; func paintUniverse(timer : NSTimer)