I'm trying to do this:
catch LocksmithError.Duplicate, LocksmithError.Allocate {...}
But I get an error at ,
saying :
Expected '{' after 'catch' pattern
Does this mean you can't combine cases like case expression2, expression3 :
? Any reason it's made this way?
No, it's currently not possible to combine multiple patterns in a catch
clause – the grammar (as detailed by the Swift Language Guide) only allows for a single pattern to match against:
catch-clause → catch patternopt where-clauseopt code-block
Another possible solution to the ones proposed already, as long as your error enum is trivial (which LocksmithError
appears to be), is to use a where
clause after a binding pattern:
catch let error as LocksmithError where error == .Duplicate || error == .Allocate {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With