Swift 2.0 - 二進(jìn)制運(yùn)算符“|”不能應(yīng)用于兩個UIUserNotificationType操作數(shù)我試圖以這種方式注冊本地通知的應(yīng)用程序: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貢獻(xiàn)1802條經(jīng)驗 獲得超5個贊
在Swift 2中,您通常會執(zhí)行此操作的許多類型已更新為符合OptionSetType協(xié)議。這允許使用類似數(shù)組的語法,在您的情況下,您可以使用以下內(nèi)容。
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)UIApplication.sharedApplication().registerUserNotificationSettings(settings)
在相關(guān)的說明中,如果要檢查選項集是否包含特定選項,則不再需要使用按位AND和nil檢查。您可以簡單地詢問選項集是否包含特定值,就像檢查數(shù)組是否包含值一樣。
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貢獻(xiàn)1836條經(jīng)驗 獲得超5個贊
您可以編寫以下內(nèi)容:
let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)

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