我正在尝试检查以确保用户已授权警报和徽章.一旦得到它,我在弄清楚如何处理当前设置时遇到了问题. let settings = UIApplication.sharedApplication().currentUserNotificationSettings()println(settings)// Pr
let settings = UIApplication.sharedApplication().currentUserNotificationSettings() println(settings) // Prints - <UIUserNotificationSettings: 0x7fd0a268bca0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> if settings.types == UIUserNotificationType.Alert // NOPE - this is the line that needs an update { println("Yes, they have authorized Alert") } else { println("No, they have not authorized Alert. Explain to them how to set it.") }您正在使用==进行检查,如果所有设置选项都包含在您要比较的值中,则只会返回true.请记住,这是一个位图枚举,您可以使用按位或|向相同的值添加其他选项.您可以使用按位&来检查特定选项是否是值的一部分.
if settings.types & UIUserNotificationType.Alert != nil { // .Alert is one of the valid options }
在Swift 2.0中,您需要使用新的表示法.您的设置集合是[UIUserNotificationType]类型的数组,因此您可以像这样检查:
if settings.types.contains(.Alert) { // .Alert is one of the valid options }