Swift 2.0-El operador binario " | " no se puede aplicar a dos operandos UIUserNotificationType
Estoy tratando de registrar mi solicitud para notificaciones locales de esta manera:
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
En Xcode 7 y Swift 2.0 - Obtengo un error Binary Operator "|" cannot be applied to two UIUserNotificationType operands
. Ayúdame, por favor.
4 answers
En Swift 2, muchos tipos para los que normalmente haría esto se han actualizado para ajustarse al protocolo OptionSetType. Esto permite una sintaxis similar a una matriz para su uso, y en su caso, puede usar lo siguiente.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
Y en una nota relacionada, si desea comprobar si un conjunto de opciones contiene una opción específica, ya no es necesario utilizar bitwise AND y una comprobación nil. Simplemente puede preguntar al conjunto de opciones si contiene un valor específico de la misma manera que verificaría si una matriz contenía un valor.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
if settings.types.contains(.Alert) {
// stuff
}
En Swift 3 , las muestras deben escribirse de la siguiente manera:
let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
Y
let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
if settings.types.contains(.alert) {
// stuff
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-19 15:53:49
Puedes escribir lo siguiente:
let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-08 22:06:30
Lo que funcionó para mí fue
//This worked
var settings = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-16 07:38:52
Esto se ha actualizado en Swift 3.
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-04 04:57:59