Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

        }

    }

}
like image 421
krish Avatar asked Oct 22 '16 23:10

krish


2 Answers

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
    }
}
like image 57
Alexander Avatar answered Nov 05 '22 14:11

Alexander


So replace every instance of UIUserNotificationType.none with []

like image 41
Duncan C Avatar answered Nov 05 '22 14:11

Duncan C