Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check strings using enums

Tags:

enums

ios

swift

i'm trying to check whether the type string is equal to the num strings, however i cant seem to figure out how i check type against the rawValues of enums. so far i've done this:

However i keep getting Enum case News not found in type String

enum ContentType: String {

    case News = "News"
    case Card = "CardStack"

    func SaveContent(type: String) {

        switch type {
            case .News:
                print("news")
            case .Card:
                print("card")

        }
    }

}
like image 223
Peter Pik Avatar asked Feb 16 '26 22:02

Peter Pik


1 Answers

You can fix this by using enum's raw value in the switch:

enum ContentType: String {

    case News = "News"
    case Card = "CardStack"

    func SaveContent(type: String) {
        switch type {
        case ContentType.News.rawValue:
            print("news")
        case ContentType.Card.rawValue:
            print("card")
        default:
            break
        }
    }

}
like image 189
Sergey Kalinichenko Avatar answered Feb 19 '26 12:02

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!