Hi,
I was wondering what the best option would be to detect subcases of a switch when using merged switch cases.
Assume I have a large enum as follows:
enum TableViewRow {
case row1
case row2
// ...
case rowX
}
And when I switch over it, I need to handle up to X cases (let's say 20 cases). So whenever possible I will merge cases into one, in order to reuse code like so:
switch row {
case .row1, case .row2, case .row3 // ...
// Do work here for all those cases
default: break
}
Let's further assume there is one thing that differentiates all the cases: A title property.
So depending if it is row1 or row7, the title is different.
So in the merged case I could switch over row again and have a default title there for the default case, however that is not optimal.
I could also define a title property in TableViewRow, but maybe not every row has a title? (Maybe a bad example for Table View Rows, but assuming I only need a title for row 1 to 7 in that case only).
What I'd love to have is $0 or similar in each case, so $0 would be the actual case that we are in, even though multiple cases can be matched.
This way I could do
switch $0 {
case row1:
title = ""
case row2:
title = ""
// ...
case row7:
title = ""
}
I know this only saves me the "default" case, however it makes no sense to have a default case that is never executed, yet I have to initialize the title variable in there.
How would you go about it? I also don't see myself doing lots of if case let .myRow = row in that subcase.
Any help appreciated
Lantua
2
Would it work better to add associated values? It seems row1-row7 are the the same as far as this enum is concerned.