加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – 没有”候选人产生预期的上下文结果类型’NSNumber?’

发布时间:2020-12-14 04:48:25 所属栏目:百科 来源:网络整理
导读:在 Xcode 8和 Swift 3上出现错误,我已经尝试过任何东西,但没有结果. 这里的代码: static func addNotificationInterval(title: String,body: String,indentifier: String,interval: Double) { let content = UNMutableNotificationContent() content.title
在 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?’

解决方法

检查 the latest reference of UNMutableNotificationContent

var badge: NSNumber?

The number to apply to the app’s icon.

在Swift 3中,删除了许多隐式类型转换,如Int到NSNumber.您需要在它们之间显式地转换类型.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读