我正在编写简单的程序 我想在 mac os x 上显示通知
这是我的代码
import Foundation
import Cocoa
var notification:NSUserNotification = NSUserNotification()
notification.title = "TEST"
notification.subtitle = "TTTT"
var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
if(notificationcenter != nil) {
notificationcenter.scheduleNotification(notification)
}
该代码构建成功但停止运行代码时
fatal error: Can't unwrap Optional.None
var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
我能做什么
最佳答案
你得到这个是因为你尝试 unwrap optional,它可以是 nil,你可以这样做:
if let notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter() {
notificationcenter.scheduleNotification(notification)
}
关于macos - Swift NotificationCenter 无法解包 Optional.None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24483942/