【问题标题】:No '+' candidates produce the expected contextual result type 'NSNumber?' Swift 3没有“+”候选产生预期的上下文结果类型“NSNumber?”斯威夫特 3
【发布时间】:2016-09-26 21:51:34
【问题描述】:

在 Xcode 8 和 Swift 3 上 + 出现错误,我尝试了任何方法但没有结果。 这里是代码:

static func addNotificationInterval(title: String, body: String,

indentifier: String, interval: Double) {

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: body, arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = UIApplication.shared.applicationIconBadgeNumber + 1; content.categoryIdentifier = "com.elonchan.localNotification"

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: true)
    let request = UNNotificationRequest.init(identifier: indentifier, content: content, trigger: trigger)
    let center = UNUserNotificationCenter.current()

    center.add(request)

    print("SetNotifiInterval")

}

错误来自 + :

content.badge = UIApplication.shared.applicationIconBadgeNumber + 1; content.categoryIdentifier = "com.elonchan.localNotification"

错误类型:

没有“+”候选产生预期的上下文结果类型“NSNumber?”

【问题讨论】:

  • 请确保您使用 CMD-K 格式化您的代码

标签: swift3 xcode8


【解决方案1】:

查看the latest reference of UNMutableNotificationContent:

var badge: NSNumber?

应用到应用图标的数字。

在 Swift 3 中,许多隐式类型转换,例如 IntNSNumber,都被删除了。您需要显式转换它们之间的类型。

content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber; ...

【讨论】:

  • 谢谢它运行:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多