【问题标题】:Cannot invoke initializer for type UNNotificationCategory when migrating to iOS10迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始化程序
【发布时间】:2016-08-09 11:39:06
【问题描述】:

我正在使用一个示例项目来处理新通知,从 Swift 2.2 过渡到 Swift 2.3,也从 iOS 9 过渡到 iOS 10。我正在使用下面的代码,取自 this example

@available(iOS 10.0, *)
func setupAndGenerateLocalNotification() {
    // Register an Actionable Notification
    let highFiveAction = UNNotificationAction(identifier: NotificationActions.HighFive.rawValue, title: "High Five", options: [])

    //next line is an error:
    let category = UNNotificationCategory(identifier: "wassup", actions: [highFiveAction], minimalActions: [highFiveAction], intentIdentifiers: [], options: [.customDismissAction])
    //Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

    UNUserNotificationCenter.current().setNotificationCategories([category])

    let highFiveContent = UNMutableNotificationContent()
    highFiveContent.title = "Wassup?"
    highFiveContent.body = "Can I get a high five?"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    let highFiveRequestIdentifier = "sampleRequest"
    let highFiveRequest = UNNotificationRequest(identifier: highFiveRequestIdentifier, content: highFiveContent, trigger: trigger)
    UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(highFiveRequest) { (error) in
        // handle the error if needed
        print(error)
    }
}

我得到的错误是

Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

【问题讨论】:

    标签: ios notifications swift2 ios10


    【解决方案1】:

    你没有提到你的错误是什么,但我怀疑你复制的代码是为比你安装的更早的测试版编写的。

    我在工作中使用的是 Swift 3,而不是 Swift 2.3。在那里,从 Xcode 8 beta 5 开始,您应该将 UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest 更改为 UNUserNotificationCenter.current().add,并从您的 UNNotificationCategory 中删除 minimalActions: [highFiveAction],。可能只有后者可能适用于 Swift 2.3?

    我希望这能让你继续前进,但请注意:这可能会在 iOS 10 最终版之前再次发生变化。特别是,我怀疑current() 可能会变成current

    【讨论】:

    • 哦抱歉,错误在代码内部(已注释掉)
    • 不,我需要 swift 2.3,而这正是我正在使用的代码(实际上我不得不修改 swift3 示例以适应 swift 2.3)
    • 你有没有按照我说的做——删除minimalActions参数?
    • 是的,确实是这样。当我读到这篇文章时,我自己已经弄清楚了……谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多