ios - 因 NSException 类型的未捕获异常而终止 Timer Swift 崩溃

标签 ios swift xcode timer nsexception

我试图在 swift 中使用计时器类,但我的应用程序不断因 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

以及线程 1:信号 SIGABRT

最佳答案

您向计时器传递了错误的目标。您想要传递 clickClass,而不是 self。并且选择器不应该引用变量,它应该引用类。

timer = Timer.scheduledTimer(timeInterval: 1, target: clickClass, selector: #selector(playSound.repeatSound), userInfo: nil, repeats: true)
<小时/>

您还应该注意正确命名事物。类、结构和枚举名称应以大写字母开头。变量、函数和大小写名称应以小写字母开头。

关于ios - 因 NSException 类型的未捕获异常而终止 Timer Swift 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592503/

相关文章:

ios - 将日期插入到 xcode 中的 sqlite3 数据库中

ios - 如何使用表格 View 制作粘性页脚和页眉?

ios - 如何在本地为 Objective-C 语言运行 travis-ci

ios - 如何确定当前的iPhone/设备型号?

Swift:无法更改imageView框架,imageView位于 Storyboard中

ios - XCTest - 如何检查 HTML 复选框的状态?

swift - "Charts"(IOS) xcode 9.0 编译错误 : "static var ' defaultFormatter ' is not public"

具有动态元素的 iOS 自定义控件

ios - Swift:使用元组的单个开关案例中的多个间隔

macos - Int32 无法转换为 gcl_queue_flags