在Xcode 8

时间:2016-09-27 20:48:15

标签: swift xcode macos

我在macOS项目中运行某种类型的代码时遇到错误,而且我不确定如何修复它。这是我得到的错误:

I'm getting this weird error.

这是给我错误的代码:

func AlarmAlertBox() {
    let alert = NSAlert()
    alert.messageText = AlarmTitleVar
    alert.informativeText = DescriptionVar
    alert.addButton(withTitle: "OK")
    let result = alert.runModal()
    switch(result) {
    case NSAlertFirstButtonReturn:
        print("OK", terminator: "")
    default:
        break
    }
}

EDIT(被要求添加此内容):我在Timer函数中运行此代码:

_ = DispatchQueue.global().async {

        var ReminderNum = 1

        self.ReminderCheckON = true

        while self.ReminderCheckON == true {
            let int : Int = ReminderNum
            let ReminderStr = String(int)
            let ReminderStuff = UserDefaults().string(forKey: ReminderStr) ?? ""
            ReminderNum = ReminderNum + 1
            if ReminderStuff == "" {
                self.ReminderCheckON = false
                ReminderNum = 1
            }

            if ReminderStuff.range(of: self.ReminderCurrentTime.stringValue) != nil
            {
                print("Alarm Time!")
                if self.DeleteAlarm == "ON" {
                    UserDefaults().set("-", forKey: ReminderStr)
                    self.AlarmsEnd()
                    self.UpcomingAlarms.stringValue = ""
                    self.ReminderList.removeAllItems()
                    self.AlarmsCheck()
                }

                self.AlarmTitleVar = UserDefaults().string(forKey: "T" + ReminderStr) ?? ""

                self.DescriptionVar = UserDefaults().string(forKey: "D" + ReminderStr) ?? ""

                self.ReminderCheckON = false

                self.ReminderRingToneVar = UserDefaults().string(forKey: "R" + ReminderStr) ?? "Alarm"

                if self.ReminderRingToneVar == "Spring" {
                    let Spring = NSSound(named: "Spring")
                    Spring!.play()
                    self.AlarmAlertBox()
                    Spring!.stop()
                }

                if self.ReminderRingToneVar == "Alarm" {
                    let Alarm = NSSound(named: "RingDing")
                    Alarm!.play()
                    self.AlarmAlertBox()
                    Alarm!.stop()
                }

                if self.ReminderRingToneVar == "Strings" {
                    let Strings = NSSound(named: "Strings")
                    Strings!.play()
                    self.AlarmAlertBox()
                    Strings!.stop()
                }

                if self.ReminderRingToneVar == "Happy" {
                    let Happy = NSSound(named: "Happy")
                    Happy!.play()
                    self.AlarmAlertBox()
                    Happy!.stop()
                }

                if self.ReminderRingToneVar == "Bouncy" {
                    let Bouncy = NSSound(named: "Joshua's Creation")
                    Bouncy!.play()
                    self.AlarmAlertBox()
                    Bouncy!.stop()
                }

                if self.ReminderRingToneVar == "Star-Spangled Banner" {
                    let StarSpangledBanner = NSSound(named: "The_United_States_Army_Old_Guard_Fife_and_Drum_Corps_-_02_-_United_States_National_Anthem_The_Star_Spangled_Banner")
                    StarSpangledBanner!.play()
                    self.AlarmAlertBox()
                    StarSpangledBanner!.stop()
                }
            }
        }
    }

调试窗口给了我这个:

  

2016-09-27 13:35:00.856685 Clock Pro [2441:115266] [一般]未被捕获的例外被提出

     

2016-09-27 13:35:00.856732 Clock Pro [2441:115266] [一般]   2016-09-27 13:35:00.856803 Clock Pro [2441:115266] [一般]   2016-09-27 13:35:00.856885 Clock Pro [2441:115266] *由于未捕获的异常终止应用程序' NSGenericException',原因:' - [NSAlert runModal]可能只是从主线程调用。其他线程上的行为未定义。'   * 第一次抛出调用堆栈:   (       0 CoreFoundation 0x00007fffaaefd52b exceptionPreprocess + 171       1 libobjc.A.dylib 0x00007fffbf5d5cad objc_exception_throw + 48       2 CoreFoundation 0x00007fffaaf7ba0d + [NSException raise:format:] + 205       3 AppKit 0x00007fffa9323b44 _NSRunModal + 103       4 AppKit 0x00007fffa8dd757d - [NSAlert runModal] + 270       5 Clock Pro 0x000000010005aab8 _TFC9Clock_Pro14ViewController13AlarmAlertBoxfT_T_ + 344       6 Clock Pro 0x000000010002d053 _TFFC9Clock_Pro14ViewController5TimerFT_T_U_FT_T_ + 6563       7 Clock Pro 0x000000010000f317 _TTRXFo ___ XFdCb _ + 39       8 libdispatch.dylib 0x00000001009fe74d _d   libc ++ abi.dylib:以NSException类型的未捕获异常终止

我在我的项目中经常使用这段代码,而且我还没有遇到其他代码行的问题。我已经尝试了几个方法来解决这个问题或解决它,但我还没有成功。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

正如Hamish已经评论过的那样,您需要在主要步骤上运行所有与UI相关的任务。这不是非常困难,使用后台线程的人应该知道:

这样的事情:

    if self.ReminderRingToneVar == "Spring" {
        DispatchQueue.main.async {
            let spring = NSSound(named: "Spring")
            spring!.play()
            self.AlarmAlertBox()
            spring!.stop()
        }
    }

顺便说一下,如果您有机会在公共场所分享您的代码,我们最好遵循Swift的标准编码规则(我的意思是,包括在Q& A网站上写一个问题) :仅对类型使用capital-CamelCase。

您的代码难以阅读。