我在ViewController.swift上使用Swift3中的UIUserNotificationType.none
,我收到了这个错误:'none'不可用user []来构造一个空选项集;这是我的代码:
func updateUI() {
let currentSettings = UIApplication.shared.currentUserNotificationSettings
if currentSettings?.types != nil {
if currentSettings!.types == [UIUserNotificationType.badge, UIUserNotificationType.alert] {
textField.isHidden = false
button.isHidden = false
datePicker.isHidden = false
}
else if currentSettings!.types == UIUserNotificationType.badge {
textField.isHidden = true
}
else if currentSettings!.types == UIUserNotificationType.none {
textField.isHidden = true
button.isHidden = true
datePicker.isHidden = true
}
}
}
答案 0 :(得分:13)
如错误所示,该.none
类型没有OptionSet
成员。只需使用[]
,空选项集。
这应该有效:
func updateUI() {
guard let types = UIApplication.shared.currentUserNotificationSettings?.types else {
return
}
if types == [.badge, .alert] {
textField.isHidden = false
button.isHidden = false
datePicker.isHidden = false
}
else if types == [.badge] {
textField.isHidden = true
}
else if types.isEmpty {
textField.isHidden = true
button.isHidden = true
datePicker.isHidden = true
}
}
更好的是,使用开关:
func updateUI() {
guard let types = UIApplication.shared.currentUserNotificationSettings?.types else {
return
}
switch types {
case [.badge, .alert]:
textField.isHidden = false
button.isHidden = false
datePicker.isHidden = false
case [.badge]:
textField.isHidden = true
case []:
textField.isHidden = true
button.isHidden = true
datePicker.isHidden = true
default:
fatalError("Handle the default case") //TODO
}
}
答案 1 :(得分:2)
所以用[]
替换UIUserNotificationType.none
的每个实例