2016-02-07 44 views
1

下面是代碼:的Xcode 7.2和的NSTimer拋出未捕獲的異常

import UIKit 

class ViewController: UIViewController { 

    var clickNumber:Int = 0 

    @IBOutlet weak var CountLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     CountLabel.text = "0" 

     let interval: Double = 60/120*4 

     var metronomeTimer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "metronomeFunc:", userInfo: nil, repeats: true) 

     func metronomeFunc(timer:NSTimer) { 
      CountLabel.text = String(clickNumber) 
      clickNumber = clickNumber+1 

     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 
} 

我不知道爲什麼,但我的新電腦上這個簡單的東西不起作用。

下面是它給了我一個錯誤:

2016-02-07 16:45:19.683 Move And Play[14743:2005023] -[Move_And_Play.ViewController metronomeFunc:]: unrecognized selector sent to instance 0x7f8c0a435c20 
2016-02-07 16:45:19.695 Move And Play[14743:2005023] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Move_And_Play.ViewController metronomeFunc:]: unrecognized selector sent to instance 0x7f8c0a435c20' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010730ee65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x000000010904edeb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010731748d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x000000010726490a ___forwarding___ + 970 
    4 CoreFoundation      0x00000001072644b8 _CF_forwarding_prep_0 + 120 
    5 Foundation       0x00000001076f40d1 __NSFireTimer + 83 
    6 CoreFoundation      0x000000010726ec84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20 
    7 CoreFoundation      0x000000010726e831 __CFRunLoopDoTimer + 1089 
    8 CoreFoundation      0x0000000107230241 __CFRunLoopRun + 1937 
    9 CoreFoundation      0x000000010722f828 CFRunLoopRunSpecific + 488 
    10 GraphicsServices     0x000000010bba3ad2 GSEventRunModal + 161 
    11 UIKit        0x0000000107b2b610 UIApplicationMain + 171 
    12 Move And Play      0x0000000106e2c17d main + 109 
    13 libdyld.dylib      0x0000000109b8592d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

它總是工作過我的舊電腦與舊版本的Xcode

回答

1

方法符合目標/選擇器模式的必須聲明的班級的頂級水平。

class ViewController: UIViewController { 

... 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    CountLabel.text = "0" 

    let interval: Double = 2.0 // 60/120*4 

    var metronomeTimer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "metronomeFunc:", userInfo: nil, repeats: true) 
    } 

    func metronomeFunc(timer:NSTimer) { 
    CountLabel.text = String(clickNumber) 
    clickNumber = clickNumber+1 
    } 

... 

} 

它確實無法在您的舊計算機上嵌套語法。

相關問題