Changes to registerUserNotificationSettings in Swift 2?

Adam Johnson picture Adam Johnson · Jun 14, 2015 · Viewed 8.9k times · Source

I can't seem to find any documentation on registerUserNotificationSettings beyond what was produced last November (here), but my old code doesn't seem to work for me any more in Xcode 7 and Swift 2.

I have this code in 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>))

I now receive the following two errors on the final line of code:

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

and

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

I've searched for information on any changes, but I can't find anything. Am I missing something obvious?

Answer

Bannings picture Bannings · Jun 14, 2015

Instead of using (NSSet(array: [restartGameCategory])) as Set<NSObject>) with (NSSet(array: [restartGameCategory])) as? Set<UIUserNotificationCategory>) like so:

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