以 NSException Timer Swift Crash 类型的未捕获异常终止
Posted
技术标签:
【中文标题】以 NSException Timer Swift Crash 类型的未捕获异常终止【英文标题】:Terminating With Uncaught Exception Of Type NSException Timer Swift Crash 【发布时间】:2019-06-14 06:21:40 【问题描述】:我正在尝试快速使用计时器类,但我的应用程序不断因信号 SIGBART 而崩溃,并且还以未捕获的类型为 NSException 错误的异常终止。请帮忙。谢谢。
我已经把它缩小到因为我的计时器,但我不知道如何解决它。
import UIKit
import AVFoundation
class playSound
var audioPlayer:AVAudioPlayer?
var bpm: Int
var switchOn: Bool
init(switchOn: Bool, bpm: Int)
self.bpm = bpm
self.switchOn = switchOn
func startSound()
self.switchOn = true
func changeBpm(bpm:Int)
self.bpm = bpm
func stopSound()
self.audioPlayer?.pause()
self.switchOn = false
@objc func repeatSound()
DispatchQueue.global(qos: .background).async
while (true)
let sleepAmount:Float = Float(abs((60/self.bpm)-1))
//print(self.bpm)
//print(sleepAmount)
if (self.switchOn == true)
print("hello")
let url = Bundle.main.url(forResource: "click", withExtension: "mp3")
guard url != nil else
return
do
self.audioPlayer = try AVAudioPlayer(contentsOf: url!)
self.audioPlayer?.play()
print(self.bpm)
catch
print("error")
class ViewController: UIViewController
var timer = Timer()
@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var stepperView: UIStepper!
@IBOutlet weak var sliderView: UISlider!
var clickClass = playSound(switchOn: false, bpm: 120)
override func viewDidAppear(_ animated: Bool)
clickClass.repeatSound()
@IBAction func `switch`(_ sender: UISwitch)
do
if sender.isOn == true
// this is the code causing the error
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(clickClass.repeatSound), userInfo: nil, repeats: true)
print("play")
else
timer.invalidate()
clickClass.stopSound()
print("pause")
我希望应用每秒播放一次声音。 错误是:
2019-06-13 22:49:38.226089-0700 Metronome[6695:2566182] -[Metronome.ViewController repeatSound]: unrecognized selector sent to instance 0x145e11dc0
2019-06-13 22:49:38.229502-0700 Metronome[6695:2566182] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Metronome.ViewController repeatSound]: unrecognized selector sent to instance 0x145e11dc0'
*** First throw call stack:
(0x18144127c 0x18061b9f8 0x18135dab8 0x1adc27f60 0x181446ac4 0x18144875c 0x181ec88a4 0x1813d3650 0x1813d3380 0x1813d2bb4 0x1813cdb04 0x1813cd0b0 0x1835cd79c 0x1adbfc978 0x100ffccf0 0x180e928e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
连同Thread 1: signal SIGABRT
【问题讨论】:
【参考方案1】:您将错误的目标传递给计时器。你想通过clickClass
,而不是self
。并且选择器不应该引用变量,它应该引用类。
timer = Timer.scheduledTimer(timeInterval: 1, target: clickClass, selector: #selector(playSound.repeatSound), userInfo: nil, repeats: true)
您还应该注意正确命名事物。类、结构和枚举名称应以大写字母开头。变量、函数和案例名称应以小写字母开头。
【讨论】:
对于第一个选项,我收到错误消息。请在实际代码中检查一次,如果出现错误,请更新您的答案。 对于第一个选项'使用未解析的标识符'repeatSound''。第二个选项是正确的。【参考方案2】:在您的代码中为计时器设置的目标不正确。将目标更改为self.clickClass
,如下所示:
timer = Timer.scheduledTimer(timeInterval: 1, target: self.clickClass, selector: #selector(clickClass.repeatSound), userInfo: nil, repeats: true)
对于self
目标,应用程序尝试在同一视图控制器中查找repeatSound
方法。另外,请按照以下链接了解快速命名约定的最佳做法:
https://github.com/raywenderlich/swift-style-guide
【讨论】:
我已经在编码中检查了它,它对我来说工作正常。你可以自己检查一下。以上是关于以 NSException Timer Swift Crash 类型的未捕获异常终止的主要内容,如果未能解决你的问题,请参考以下文章
Swift 项目以 NSException 类型的未捕获异常终止
Swift 2 到 3 迁移错误(libc++abi.dylib:以 NSException 类型的未捕获异常终止)
从 Swit2.3 到 Swift3 的转换导致“以 NSException 类型的未捕获异常终止”
(Swift 3) 父子上下文崩溃核心数据 (libc++abi.dylib: 以 NSException (Recorded Frame) 类型的未捕获异常终止)