Swift 2 中对 registerUserNotificationSettings 的更改?

Changes to registerUserNotificationSettings in Swift 2?

除了去年 11 月生成的文档 (here),我似乎找不到任何关于 registerUserNotificationSettings 的文档,但我的旧代码在 Xcode 中似乎不再适用7 和 Swift 2.

我在 App Delegate 中有这段代码:

let endGameAction = UIMutableUserNotificationAction()
endGameAction.identifier = "END_GAME"
endGameAction.title = "End Game"
endGameAction.activationMode = .Background
endGameAction.authenticationRequired = false
endGameAction.destructive = true

let continueGameAction = UIMutableUserNotificationAction()
continueGameAction.identifier = "CONTINUE_GAME"
continueGameAction.title = "Continue"
continueGameAction.activationMode = .Foreground
continueGameAction.authenticationRequired = false
continueGameAction.destructive = false

let restartGameCategory = UIMutableUserNotificationCategory()
restartGameCategory.identifier = "RESTART_CATEGORY"
restartGameCategory.setActions([continueGameAction, endGameAction], forContext: .Default)
restartGameCategory.setActions([endGameAction, continueGameAction], forContext: .Minimal)

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: (NSSet(array: [restartGameCategory])) as Set<NSObject>))

我现在在代码的最后一行收到以下两个错误:

'Element.Protocol' does not have a member named 'Alert'

Cannot invoke 'registerUserNotificationSettings' with an argument list of type '(UIUserNotificationSettings)'

我搜索了有关任何更改的信息,但找不到任何信息。我是否遗漏了一些明显的东西?

而不是像这样将 (NSSet(array: [restartGameCategory])) as Set<NSObject>)(NSSet(array: [restartGameCategory])) as? Set<UIUserNotificationCategory>) 一起使用:

application.registerUserNotificationSettings(
    UIUserNotificationSettings(
        forTypes: [.Alert, .Badge, .Sound],
        categories: (NSSet(array: [restartGameCategory])) as? Set<UIUserNotificationCategory>))

@Banning 的回答会奏效,但可以用更快捷的方式来做到这一点。您可以使用具有通用类型 UIUserNotificationCategory.

的 Set 从头开始​​构建它,而不是使用 NSSet 和向下转换
let categories = Set<UIUserNotificationCategory>(arrayLiteral: restartGameCategory)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
application.registerUserNotificationSettings(settings)

同样值得注意的是,将代码分成多行将有助于您准确确定问题所在。在这种情况下,您的第二个错误只是第一个错误的结果,因为表达式是内联的。

正如@stephencelis 在他下面的评论中专家指出的那样,集合是 ArrayLiteralConvertible,因此您可以将其一直减少到以下。

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: [restartGameCategory])