[英]Terminating With Uncaught Exception Of Type NSException Timer Swift Crash
I am trying to use the timer class in swift, but my app keeps crashing with the signal SIGBART and also terminating with uncaught exception of type NSException errors. 我试图在swift中使用timer类,但是我的应用程序一直在使用SIGBART信号崩溃,并且还终止了类型NSException错误的未捕获异常。 Please help.
请帮忙。 Thanks.
谢谢。
I have narrowed it down to being because of my timer, but I do not know how to fix it. 我把它缩小到因为我的计时器,但我不知道如何解决它。
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")
}
}
}
}
I expect the app to play the sound every second. 我希望应用程序每秒播放声音。 The is the error is:
错误是:
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
along with Thread 1: signal SIGABRT
以及
Thread 1: signal SIGABRT
You are passing the wrong target to the timer. 您将错误的目标传递给计时器。 You want to pass
clickClass
, not self
. 你想传递
clickClass
,而不是self
。 And the selector should not reference the variable, it should reference the class. 并且选择器不应该引用变量,它应该引用类。
timer = Timer.scheduledTimer(timeInterval: 1, target: clickClass, selector: #selector(playSound.repeatSound), userInfo: nil, repeats: true)
You should also take care to name things properly. 您还应该注意正确命名。 Class, struct, and enum names should start with uppercase letters.
类,结构和枚举名称应以大写字母开头。 Variable, function, and case names should start with lowercase letters.
变量,函数和案例名称应以小写字母开头。
The target set in your code for timer is incorrect. 您的代码中为计时器设置的目标不正确。 Change the target to
self.clickClass
as below : 将目标更改为
self.clickClass
,如下所示:
timer = Timer.scheduledTimer(timeInterval: 1, target: self.clickClass, selector: #selector(clickClass.repeatSound), userInfo: nil, repeats: true)
With self
target the application tries to find the repeatSound
method in the same viewcontroller. 使用
self
目标,应用程序尝试在同一个viewcontroller中找到repeatSound
方法。 Also please follow below link for the best practices for swift naming conventions : 另外,请按照以下链接获取swift命名约定的最佳实践:
https://github.com/raywenderlich/swift-style-guide https://github.com/raywenderlich/swift-style-guide
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.