7

UIUserNotificationType.none在 ViewController.swift 上使用 Swift3 中的,我得到了这个错误: 'none' is available user[] to construction an empty option set ; 这是我的代码:

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

        }

    }

}
4

2 回答 2

15

正如错误所说,该类型没有.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
    }
}
于 2016-10-22T23:10:14.490 回答
3

UIUserNotificationType.none所以用 []替换每个实例

于 2016-10-22T23:11:32.397 回答