Imagine you have an enum :
enum ConnectionError: Error {
case timeout
case badRequest
}
If you are checking an error from a result, you will do it like this:
switch (error) {
case .timeout:
print("timeout")
case .badRequest:
print("bad request")
default:
print("another request")
As frameworks and libraries develop, the ConnectionError enum could be extended by a future version of a framework or library. Adding another case to the enum could look like this:
enum ConnectionError: Error {
case timeout
case badRequest
case denied
}
If a newer version of a framework updates this, it will cause your old code to select the default case, and you would not even notice that there is a new case. By adding a little attribute, @unkown, to the default case, you tell the compiler to warn you if the switch block is no longer exhaustive. This is a huge benefit to making sure enum cases are exhaustive even when new cases come along later on. Our switch statement now looks like this:
switch (error) {
case .timeout:
print("timeout")
case .badRequest:
print("bad request")
@unkown default:
print("an unkown request request")
The added case for the enum will now cause a compiler warning—but it won't break our code. This is important as this might happen in other external libraries as well. So, you don't need to worry about it in terms of compiling your software, but it is beneficial to be informed about this change. Swift 4.2 would not notify you nor would it break the code.