gpt4 book ai didi

ios - 如何调试以 NSException 类型的未捕获异常终止

转载 作者:行者123 更新时间:2023-11-28 12:54:35 27 4
gpt4 key购买 nike

我在 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

最佳答案

从您的错误日志中, View Controller 尝试使用 NSTimer 调用选择器 spawnEnemy:

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

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

关于ios - 如何调试以 NSException 类型的未捕获异常终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com