In addition, it was previously noted that if case pattern matching doesn't make sense when used with unknown pattern, as 'there is no way to prevent it from producing a warning'.
But if you treat switch as a sequence of if-else statements, it becomes clear that:
- it doesn't make sense to use the
unknownpattern in the firstcaseof aswitchstatement as well; - it makes perfect sense to use the
unknownpattern after all known cases were considered in the sequence ofif-elsestatements.
struct Example {
func inspect(paperSize newValue: PaperSize) {
if newValue == .usLetter || newValue == .a4 {
print("usLetter or a4")
// use normal pattern matching for the known enum case
} else if case .photo4x6 = newValue {
print("photo4x6")
// use new pattern matching for the unknown enum case
// no warning produced, because all known enum cases have been matched
} else if case ._(let rawValue) = newValue {
print("an unknown case with an associated value \(rawValue)")
}
}
}