2015-06-10 17 views
181

sto cercando di registrare il mio applicazione per le notifiche locali in questo modo:Swift 2.0 - Operatore binario "|" Non può essere applicato a due operandi UIUserNotificationType

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil)) 

in Xcode 7 e Swift 2.0 - ottengo l'errore Binary Operator "|" cannot be applied to two UIUserNotificationType operands. Mi aiuti per favore.

+2

circonda con "()" funziona per me UIApplication.sharedApplication() registerUserNotificationSettings (UIUserNotificationSettings (forTypes:. (UIUserNotificationType .Alert | UIUserNotificationType.Badge), categorie: nil)) –

+1

Ora ho: 'Impossibile trovare un sovraccarico '|' che accetta gli argomenti forniti ' –

+0

Non ho un'altra idea, mi dispiace. –

risposta

386

In Swift 2, molti tipi che si dovrebbero fare in questo modo sono stati aggiornati per conformarsi al protocollo OptionSetType. Ciò consente la sintassi dell'array come per l'uso e, nel tuo caso, puoi usare quanto segue.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil) 
UIApplication.sharedApplication().registerUserNotificationSettings(settings) 

E su una nota correlata, se si vuole verificare se un gruppo di opzioni contiene una specifica opzione, non è più necessario utilizzare AND bit a bit e un assegno pari a zero. Puoi semplicemente chiedere al set di opzioni se contiene un valore specifico nello stesso modo in cui verificherai se un array contiene un valore.

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil) 

if settings.types.contains(.Alert) { 
    // stuff 
} 

In Swift 3, i campioni devono essere scritte come segue:

let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil) 
UIApplication.shared.registerUserNotificationSettings(settings) 

e

let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil) 

if settings.types.contains(.alert) { 
    // stuff 
} 
+1

Che dire se hai '' 'flags | = .Alert?' '' Puoi usare '' 'flags = [flags, .Alert]' ''? – user3246173

+0

i.e Viene trattato come un Set in cui i valori sono univoci o come una matrice che potrebbe portare a un valore finale errato? – user3246173

+0

@ user3246173 Dipende da come viene dichiarata la variabile flags. Se il tipo di flag è dichiarato esplicitamente come 'UIUserNotificationType', ovvero' var flags: UIUserNotificationType = [.Alert, .Badge] ', sarà trattato come un set, e potresti aggiungere un elemento usando i metodi di istanza impostati come' insert() ',' union() ',' unionInPlace() ', o con l'approccio che hai menzionato senza preoccuparti dei duplicati. –

35

È possibile scrivere il seguente:

let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge) 
+9

Troppo complicato. –

+0

hai ragione :) –

+1

wow, questo sembra orribile! 'NSTrackingAreaOptions.MouseEnteredAndExited.union (NSTrackingAreaOptions.MouseMoved) .union (NSTrackingAreaOptions.ActiveAlways)', ma grazie per una soluzione funzionante –

7

Che cosa ha funzionato per me era

//This worked 
var settings = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil) 
+9

sembra quasi esattamente la risposta accettata sopra. Consideri come un commento? –

+0

Questo ha funzionato anche per me –

2

Questo è stato aggiornato a Swift 3.

 let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     UIApplication.shared.registerUserNotificationSettings(settings) 
Problemi correlati