In Swift, I have a enum
and switch-statement like below:
enum Job {
case all
case sweep
case clean
case wash
}
let job: Job = .all
switch job {
case .all:
print("should do all the jobs below")
case .sweep:
print("sweep")
case .clean:
print("clean")
case .wash:
print("wash")
}
My question is that how can I modify the switch-statement so that it loops all the cases if the given job
is .all
.
So that the .all
case's printed result should be:
sweep
clean
wash
I come up an idea with:
switch job {
case .all: fallthrough
case .sweep:
print("sweep")
if job == .all { fallthrough }
case .clean:
print("clean")
if job == .all { fallthrough }
case .wash:
print("wash")
}
and wondering if there is any more 'beautiful' solution. Thanks.