Swift 2.0 - 二元运算符“|”不能应用于两个UIUserNotificationType操作数 [英] Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands

查看:193
本文介绍了Swift 2.0 - 二元运算符“|”不能应用于两个UIUserNotificationType操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种方式注册本地通知应用程序:

I am trying to register my application for local notifications this way:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

在Xcode 7和Swift 2.0中 - 我收到错误二元运算符|不能应用于两个UIUserNotificationType操作数。请帮助我。

In Xcode 7 and Swift 2.0 - I get error Binary Operator "|" cannot be applied to two UIUserNotificationType operands. Please help me.

推荐答案

在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
}

这篇关于Swift 2.0 - 二元运算符“|”不能应用于两个UIUserNotificationType操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆