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")
}
}
}
You are trying to write a switch from your String class which is not correct. You should update the SaveContent method with:
if let type = ContentType(rawValue: type) {
switch type {
case .News:
print("news")
case .Card:
print("card")
}
}
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.