我正在尝试通过以下方式为本地通知注册我的应用程序:
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。请帮我。
Binary Operator "|" cannot be applied to two UIUserNotificationType operands
在Swift 2中,通常要执行此操作的许多类型已更新为符合OptionSetType协议。这允许使用类似数组的语法,并且在您的情况下,可以使用以下语法。
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings)
与此相关的是,如果要检查选项集是否包含特定选项,则不再需要使用按位与和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 }