UIRemoteNotificationType 在 Swift 2 中无法正常工作

UIRemoteNotificationType not working properly in Swift 2

这是 Swift 中给我的代码:

 if application.respondsToSelector("registerUserNotificationSettings:") {
        let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    } else {
        let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
        application.registerForRemoteNotificationTypes(types)
    }

我在第二行收到一个错误,告诉我:

| is not a prefix unary operator

这是什么意思?

我在第 7 行也收到错误提示:

Binary Operator | cannot be applied to UIRemoteNotificationType

有人可以帮助我更好地理解这一点吗?我对发生的事情一无所知。

在 iOS 9 中,apple 没有给你使用 | 的选项,你需要做的是将 [.Bagde ,.Alert, .Sound] 分组到一种数组中

只需更改此行

 let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound

 let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

  let types = [.Badge,.Alert ,.Sound] 
 let userNotificationTypes =  [.Alert ,.Badge ,.Sound]