我正在编写简单的程序 我想在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()
我能做什么
答案 0 :(得分:0)
你得到这个,因为你尝试解包可选的,可以是零,你可以这样做:
if let notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter() {
notificationcenter.scheduleNotification(notification)
}
答案 1 :(得分:0)
您将变量声明为显式。
var notificationcenter:NSUserNotificationCenter
这意味着不能为零。
但是这个
NSUserNotificationCenter.defaultUserNotificationCenter()
失败,意味着返回。
试试这个
var notificationcenter: NSUserNotificationCenter? = NSUserNotificationCenter.defaultUserNotificationCenter()
您的var是可选,现在可以设置为nil 。