Enums and Pattern Matching
Where Patterns
Pattern cases can add conditions with where.
Match and filter
where_patterns.swift
enum Score {
case points(Int)
}
let value =
let score = Score.points(value)
var band = ""
switch score {
case Score.points(let amount) where amount >= 90:
band = "excellent"
case Score.points(let amount) where amount >= 70:
band = "steady"
case Score.points:
band = "practice"
}
print("value=\(value)")
print("band=\(band)")
enum Score {
case points(Int)
}
let value =
let score = Score.points(value)
var band = ""
switch score {
case Score.points(let amount) where amount >= 90:
band = "excellent"
case Score.points(let amount) where amount >= 70:
band = "steady"
case Score.points:
band = "practice"
}
print("value=\(value)")
print("band=\(band)")
enum Score {
case points(Int)
}
let value =
let score = Score.points(value)
var band = ""
switch score {
case Score.points(let amount) where amount >= 90:
band = "excellent"
case Score.points(let amount) where amount >= 70:
band = "steady"
case Score.points:
band = "practice"
}
print("value=\(value)")
print("band=\(band)")
where
A `where` clause refines a pattern after it has matched the shape of the value.