且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift 2.0 - 二元运算符“|"不能应用于两个 UIUserNotificationType 操作数

更新时间:2021-11-10 21:36:03

在 Swift 2 中,您通常会对其执行此操作的许多类型已更新以符合 OptionSetType 协议.这允许使用类似数组的语法,在您的情况下,您可以使用以下内容.

In Swift 2, many types that you would typically do this for have been updated to conform to the OptionSetType protocol. This allows for array like syntax for usage, and In your case, you can use the following.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

在相关说明中,如果您想检查选项集是否包含特定选项,则不再需要使用按位 AND 和 nil 检查.您可以简单地询问选项集是否包含特定值,就像检查数组是否包含值一样.

And on a related note, if you want to check if an option set contains a specific option, you no longer need to use bitwise AND and a nil check. You can simply ask the option set if it contains a specific value in the same way that you would check if an array contained a value.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)

if settings.types.contains(.Alert) {
    // stuff
}

Swift 3 中,示例必须编写如下:

In Swift 3, the samples must be written as follows:

let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)

let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)

if settings.types.contains(.alert) {
    // stuff
}