我在编写简单的程序
我想在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)
}