我有这个代码使用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>)
答案 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)