我正在编写简单的程序,我想在mac 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()我能做什么
发布于 2014-06-30 08:00:04
您可以这样做,因为您可以尝试展开可选的(可以是零),您可以这样做:
if let notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter() {
notificationcenter.scheduleNotification(notification)
}发布于 2014-06-30 08:16:37
您将变量声明为显式变量。
var notificationcenter:NSUserNotificationCenter这意味着不可能是零。
但这是
NSUserNotificationCenter.defaultUserNotificationCenter()是否会失败,这意味着将返回零。
尝尝这个
var notificationcenter: NSUserNotificationCenter? = NSUserNotificationCenter.defaultUserNotificationCenter()您的变量是可选的,现在可以将设置为零。
https://stackoverflow.com/questions/24483942
复制相似问题