为了账号安全,请及时绑定邮箱和手机立即绑定

Swift 2.0 - 二进制运算符“|”不能应用于两个UIUserNotificationType

Swift 2.0 - 二进制运算符“|”不能应用于两个UIUserNotificationType

慕哥9229398 2019-07-30 11:13:57
Swift 2.0 - 二进制运算符“|”不能应用于两个UIUserNotificationType操作数我试图以这种方式注册本地通知的应用程序:UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))在Xcode 7和Swift 2.0中 - 我收到错误Binary Operator "|" cannot be applied to two UIUserNotificationType operands。请帮我。
查看完整描述

3 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

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

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

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

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)if settings.types.contains(.Alert) {
    // stuff}

Swift 3中,样本必须写成如下:

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}


查看完整回答
反对 回复 2019-07-30
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

您可以编写以下内容:

let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)


查看完整回答
反对 回复 2019-07-30
?
慕容3067478

TA贡献1773条经验 获得超3个赞

对我有用的是


//This worked

var settings = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)


查看完整回答
反对 回复 2019-07-30
  • 3 回答
  • 0 关注
  • 588 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信