It is used for better readability of enum statements
Below is a switch statement paired with an enum
enum Phone: String {
case iphone11pro
case iphonese
case pixel
case nokia
}
func getSeansOpinion(on phone: Phone) {
switch phone {
case .iphone11pro:
print("Great overall phone")
case .iphonese:
print("its alrigth, but a small phone")
case .pixel:
print("great camera")
case .nokia:
print("great durability")
}
}
getSeansOpinion(on: .iphone11pro)
output
"Great overall phone"
If you are using switch statement without enum you have to have a default statement
let matchmakingRank = 23
func determinePlayerLeague(from rank: Int) {
switch rank {
case 0:
print("Play the game to determine your league")
case 1..<50:
print("You are in BRONZE League")
case 50..<100:
print("You are in SILVER League")
case 100..<200:
print("You are in GOLD League")
default:
print("You are in a League of your own. We don't know where you are")
}
}
determinePlayerLeague(from: matchmakingRank)
output:
You are in BRONZE League