如何使用NSException类型的未捕获异常调试终止

时间:2016-02-12 14:07:01

标签: ios xcode swift debugging

我在Xcode 7中运行Swift 2.我看了几个人在他们的应用程序崩溃后调试此错误的例子,但无法用我自己的应用程序来计算如何调试它。虽然我不太习惯使用它们,但在包含一些断点后,我认为错误位于代码的中上部。以下是我的代码,任何帮助都会很棒!我是初学者,所以不要太复杂,谢谢。

import UIKit

class ViewController: UIViewController {

    func randomPoint() -> CGPoint {
        let randomPoint: CGPoint = CGPoint(x:CGFloat(arc4random()%320),y:CGFloat(arc4random()%568))
        return randomPoint
    }

    func randomColor() -> UIColor {
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }

    func spawnEnemy() {
        let enemy: UIButton = UIButton(frame: CGRect(x: 160, y: 160, width: 50, height: 50))
        enemy.backgroundColor = randomColor()
        enemy.center = randomPoint()
        enemy.addTarget(self, action: Selector("buttonPushed:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(enemy)
    }

    func buttonPushed(sender : UIButton) {
        sender.backgroundColor = UIColor.whiteColor()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnEnemy:"), userInfo: nil, repeats: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}

我得到的错误是:

2016-02-12 08:31:47.665 app[84179:14802824] -[app.ViewController spawnEnemy:]: unrecognized selector sent to instance 0x7fc7c94474a0
2016-02-12 08:31:47.687 app[84179:14802824] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[app.ViewController spawnEnemy:]: unrecognized selector sent to instance 0x7fc7c94474a0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000107ed0e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000109c10deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000107ed948d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000107e2690a ___forwarding___ + 970
    4   CoreFoundation                      0x0000000107e264b8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001082b60d1 __NSFireTimer + 83
    6   CoreFoundation                      0x0000000107e30c84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x0000000107e30831 __CFRunLoopDoTimer + 1089
    8   CoreFoundation                      0x0000000107df2241 __CFRunLoopRun + 1937
    9   CoreFoundation                      0x0000000107df1828 CFRunLoopRunSpecific + 488
    10  GraphicsServices                    0x000000010c4e3ad2 GSEventRunModal + 161
    11  UIKit                               0x00000001086ed610 UIApplicationMain + 171
    12  app                                 0x0000000107cf343d main + 109
    13  libdyld.dylib                       0x000000010a71992d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:2)

在错误日志中,视图控制器尝试使用spawnEnemy:调用选择器NSTimer

但你声明函数spawnEnemy而不需要任何参数 将您的选择器调用更改为此。

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)