openlang

Swift 2.0 用 | 运算符合并通知标志时失败

我想用这段代码为应用注册本地通知。

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

使用 Swift 2.0 和 Xcode 7 时,错误是 Binary Operator "|" cannot be applied to two UIUserNotificationType operands。有人能帮忙吗?

从英语翻译查看原文

后续讨论 4

顺着彼此的依据读下去
  1. 固定链接

    Swift 2 修改了许多通常这样使用的类型,让它们改为实现 OptionSetType 协议。因此它们支持类似数组的语法,你的这个例子可以写成下面这样。

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

    另外,检查一个选项是否属于某个选项集时,也不再需要先做按位 AND 再检查 nil。可以直接询问选项集是否包含这个值,就像检查数组中有没有某个元素一样。

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

    在 Swift 3 中,这些例子应当这样写。

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

    以及

      let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
    
      if notificationOptions.types.contains(.alert) {
          // handle this option
      }
    从英语翻译查看原文
  2. 固定链接

    这个版本对我有用

      //Successful version
      var notificationOptions = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)
    从英语翻译查看原文
  3. 固定链接

    Swift 3 改变了这里的语法。

              let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
              UIApplication.shared.registerUserNotificationSettings(notificationOptions)
    从英语翻译查看原文

同一个问题的其他角度

  1. 在 Java Mail 中显示发件人名称而非邮箱地址

    我通过 Java Mail API 发送消息。邮件到达后,收件箱显示的是发件地址。我想用一些文本替代这个地址。这应该是可以的,因为我收到的 facebook 邮件在收件箱里显示 "Facebook - Message title",而不是 "mail…

    回复 2最近活动 java
  2. 从 TypeScript 访问 Dynamics CRM

    我的 CRM 代码在网页资源中运行,所以要获取实体数据,就得写成 这样来读取数据。 可惜用 typescript 编写时,这个表达式无法编译。现在我手动把 window.parent 加到 Xrm.Page 前面……(Xrm 类型定义已经装好了),这…

    回复 2最近活动 typescript
  3. Gleam 的 HTTP 响应头需要怎样的日期格式?

    我想发送一个 Date 响应头。所需的格式如下。 这是我目前尝试的代码。

    回复 3最近活动 gleam