2016-03-07 203 views
-1

當我通過運行此代碼,它甚至運行的NSTimer首次之前,它崩潰,輸出:「的libC++ abi.dylib:與類型NSException的未捕獲的異常終止」在控制檯中。我幾天前一直在想這個問題,並且找不到解決方案,歡迎提供任何幫助,謝謝。這個NSTimer爲什麼會崩潰?

編輯:該代碼位於一個IBAction內,其目的是將集合視圖中的某些單元格顏色更改爲白色或黑色,具體取決於其在數組simulatedUniverse [universeCounter]中的通信整數值。在我添加NSTimer之前,它正在工作,但是由於屏幕更新速度太快,我只能看到最後一個顯示的圖案,對不起之前沒有添加。

控制檯輸出: 2016年3月6日23:44:54.935 Digiverse [33095:2612091] - [Digiverse.SimulatorViewController paintUniverse]:無法識別的選擇發送到實例0x7f91c504c6c0 2016年3月6日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 ___forwarding_ + 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 GSEventRun Modal + 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) 

} 
+0

@matt對不起,由於缺乏上下文,請檢查編輯,這是什麼意思是堅果? – PKLJ

+0

崩潰的行:「class AppDelegate:UIResponder,UIApplicationDelegate {」。而整個控制檯的輸出是在編輯 – PKLJ

+0

@matt我的錯誤,你最後的答案有用,非常感謝。 – PKLJ

回答

1

在崩潰日誌最顯著信息是

終止應用程序由於終止未捕獲異常'NSInvalidArgumentException',原因:' - [Digiverse.SimulatorViewController paintUniverse]:無法識別的選擇器發送到實例0x7f91c504c6c0'

在定時器聲明行選擇器paintUniverse期望的類的頂層不帶參數的方法func paintUniverse()

你的功能paintUniverse具有不能被反正用作定時器選擇三個參數。

定時器選擇器可以沒有參數在所有:

selector: "paintUniverse" - >func paintUniverse()

或一個單一的參數,該參數必須是NSTimer實例和selector必須有一個尾隨冒號:

selector: "paintUniverse:" - >func paintUniverse(timer : NSTimer)