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}

一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
您可以編寫以下內容:
let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
對我有用的是
//This worked
var settings = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)
- 3 回答
- 0 關注
- 679 瀏覽
添加回答
舉報
0/150
提交
取消