如何进行委派,即swift中的NSUserNotificationCenterDelegate

推荐答案

它与obj-c没有太大区别.

class MyClass: NSUserNotificationCenterDelegate

实现如下所示:

// NSUserNotificationCenterDelegate implementation
func userNotificationCenter(center: NSUserNotificationCenter, didDeliverNotification notification: NSUserNotification) {
    //implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
    //implementation
}

func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
    //implementation
    return true
}

当然,您必须设置委托.例如:

NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;

Swift相关问答推荐

避免嵌套导航TabView

在SWIFT中使用Objective-C struct 时出错(在作用域中找不到类型)

expose 不透明的底层类型

如何在 SwiftUI 中为过渡动画保持相同的标识

将 Encodable 扩展添加到 Swift 嵌套枚举

如何在 SWIFTUI 中旋转修剪?

UUID 哈希值是不确定的吗?

Swift - 订阅视图之外的绑定值

P384 公钥获取IncorrectParameterSize

`@available` 属性在 macOS 版本上被错误地限制

如何在 Swift 中将 XPC 自定义类列入白名单?

为什么更改属性后动画会加速? SwiftUI

SwiftUI 文本被意外截断

Swift 2.0 方法不能标记为@objc,因为参数的类型不能在 Objective-C 中表示

当我必须在 Swift 中的类、 struct 和枚举之间进行 Select 时,我应该如何推理?

Swift 函数 vs 惰性变量 vs 计算(computed)属性 - 区别?

AES 加密和解密

如何为多个类 Swift 进行扩展

UITableViewCell 不显示 detailTextLabel.text - Swift

如何使用 Swift 将文本文件逐行加载到数组中?