无法使用类型的参数列表调用类型为“UIUserNotificationSettings”的初始化程序

时间:2015-10-08 05:40:20

标签: ios swift2

我有这个代码使用Swift 1.2,然后我升级,它打破了。在下面的屏幕截图中,我有Swift 1.2和Swift 2版本。

// Register the notification settings.
//swift1.2
//let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as! Set<NSObject>)

//swift2
let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as Set<NSObject>)

enter image description here

1 个答案:

答案 0 :(得分:1)

这是因为初始化程序需要Set<UIUserNotificationCategory>,而不是Set<NSObject>。方法声明如下所示:

public convenience init(forTypes types: UIUserNotificationType, categories: Set<UIUserNotificationCategory>?)

所以将代码更改为:

let notificationTypes = UIUserNotificationType.Sound ...
let categoriesForSettings: Set<UIUserNotificationType> = ...
let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings)