Swift Compiler Error: The enum case has a single tuple as an associated value, but there are several patterns here
I found you can also silence this error by treating the associated value more like a tuple by wrapping it in an extra set of brackets:
switch result {
case .error(let err):
//
case .value((let staff, let locations)):
//
}
Ok, figured it out. Seems like enum
with associated values, where the value type is a tuple, can no longer be matched on a switch
statement like that:
// Works on Xcode 11.3.1, yields error on 11.4 (Swift 5.2)
switch result {
case .error(let err):
//
case .value(let staff, let locations):
//
}
Solution
Values from tuple have to be manually extracted in Xcode 11.4 (Swift 5.2):
// Works on Xcode 11.4
switch result {
case .error(let err):
//
case .value(let tuple):
let (staff, locations) = tuple
//
}
This is a known issue: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_release_notes
Code that relies on the compiler automatically tupling a pattern may lead to a compiler error when upgrading to Xcode 11.4, even though the code compiled before. (58425942)
For example, leaving out parentheses when switching on an Optional of a tuple type causes a compiler error:
switch x { // error: switch must be exhaustive
case .some((let a, let b), let c): // warning: the enum case has a
// single tuple as an associated value, but there are several
// patterns here, implicitly tupling the patterns and trying
// to match that instead
...
}
Workaround: Add extra parentheses to explicitly tuple the pattern:
switch x {
case .some(((let a, let b), let c)): // Notice the extra pair of parentheses.
...
}