Honey Honey - 6 months ago 41
Swift Question

Is it possible to combine catches?

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 pattern­opt ­where-clause­opt ­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 {
    // ...