Reputation: 31313
I have the following enum
which handles the UI state.
enum UIState {
case loading
case populated
case empty
case error(Error)
}
In view controllers, I use a switch
to check against each state and update the UI accordingly.
private func updateUI() {
switch state {
case .loading:
showProgressView()
case .populated, .empty:
hideProgressView()
case .error(let error):
hideProgressView()
showErrorAlert(error)
}
}
The hideProgressView()
method needs to be called in all cases except loading
. In the error
case, I am showing an alert in addition to calling the hideProgressView()
method.
As you can see there is a small duplication of code where I'm calling the hideProgressView()
method in two cases. Is there a way to consolidate the repeating method calls under one case? I tried the following way,
switch state {
case .loading:
showProgressView()
case .error(let error):
showErrorAlert(error)
case .populated, .empty, .error:
hideProgressView()
}
But I get the following warnings on the last case.
Case is already handled by previous patterns; consider removing it
Case will never be executed
Upvotes: 0
Views: 79
Reputation: 449
Maybe the easiest way would be something like this:
switch state {
case .loading:
showProgressView()
case .error(let error):
showErrorAlert(error)
fallthrough
case .populated, .empty:
hideProgressView()
}
You can get more info at: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID140
Upvotes: 2