openlang

Swift 2.0 notification flags fail when combined using the | operator

I'm attempting to register local notifications for my application using this code:

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

Using Swift 2.0 with Xcode 7, the error is Binary Operator "|" cannot be applied to two UIUserNotificationType operands. Could somebody help?

The conversation continues 4

Follow the reasoning
  1. Permalink

    Swift 2 changed many of the types commonly used this way to implement the OptionSetType protocol instead. Consequently they support a syntax resembling arrays, so your particular example can be written like this.

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

    Relatedly, checking whether an option belongs to an option set no longer requires a bitwise AND followed by testing for nil. Instead, ask whether that set contains the particular value, much as you would check an array for an element.

      let notificationOptions = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
    
      if notificationOptions.types.contains(.Alert) {
          // handle this option
      }

    For Swift 3, write those examples this way:

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

    and

      let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
    
      if notificationOptions.types.contains(.alert) {
          // handle this option
      }
  2. Permalink

    This is another possible expression:

      let notificationOptions = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)
  3. Permalink

    This version worked for me

      //Successful version
      var notificationOptions = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)
  4. Permalink

    Swift 3 changed the syntax here.

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

Other sides of the same question

  1. Making the sender appear as a label instead of an email address using Java Mail

    Im sending messages through the Java Mail API. On arrival, the inbox displays the address that sent the message. Id like some…

    Replies 2Latest activity java
  2. Dynamics CRM access from TypeScript

    Because my CRM code runs in a webresource, I have to write to retrieve the entity's data. Regrettably, that expression fails…

    Replies 2Latest activity typescript
  3. What date formatting does a Gleam HTTP response header need?

    I want to send a Date header. The required format looks like this: This is my current attempt:

    Replies 3Latest activity gleam