はじめに
プッシュ通知の初期設定でUIUserNotificationSettingsの記述方法がSwift 2.0で変わっていたのでメモ。
Swift 1.2以前
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
この記述ではSwift 2.0以降では以下のようなエラーになります。
Binary operator '|' cannot be applied to two 'UIUserNotificationType' operands
Swift 2.0以降
UIUserNotificationTypeがOptionSetTypeになり記述方法が以下のように変わりました。
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound,], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
ちなみに、OptionSetTypeは以下のように型の名前まで書くこともできますが、.以降のみ記述する方が一般的のようです。
let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound,], categories: nil)