krish krish - 1 month ago 52
Swift Question

Swift3 Xcode 8: 'none' is unavailable: use [] to construct an empty option set ; What should I do?

I was using the

UIUserNotificationType.none
in Swift3 on ViewController.swift, and I got this error: 'none' is unavailable user[] to construct an empty option set ; Here is my code:

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

}

}

}

Answer

As the error says, there is no .none member to that OptionSet type. Just use [], the empty option set.

This should work:

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
    }
}

Even better, use a switch:

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
    }
}