2016-02-07 21 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 
    } 

... 

} 

它确实无法在您的旧计算机上嵌套语法。