Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift switch statement cases have different and shared things to do

i have code like this

switch thing {
  case thisThing:
     do thing #1
     do thing #2
  case thatThing:
     do thing #2
     do thing #3
  case anotherThing:
     do thing #4
  default:
     default
}

So, EVERY case has something that ONLY IT does. Some cases also do the same things as one or more other cases.

Is there a way to accomplish this if I don't want any repetitive code?

OR, is there a much more efficient way of doing this without switch statements at all?? I mean, I could, for example, use if statements, but like switch statements, I can't think of a way to accomplish what I want without using repetitive code.

also, this example might be more clear than the above

myFavoriteNumbers = []
myLeastFavoriteNumbers = []

switch myNumber {
case 1:
  print("my number is number 1") // do this only for case 1
  myFavoriteNumbers += [1] // do this for case 1 and case 2
case 2:
  print("this is number 2") // do this only for case 2
  myFavoriteNumbers += [2] // do this for case 1 and case 2
case 3:
  print("I don't like number 3") // do this only for case 3
  myLeastFavoriteNumbers += [3] // do this for case 3 and case 4
case 4:
  print("Number Four") // do this only for case 4
  myLeastFavoriteNumbers += [4] // do this for case 3 and case 4
default:
  print("Default")
}
like image 507
rdk Avatar asked Aug 03 '16 19:08

rdk


People also ask

Can switch case have multiple conditions Swift?

Unlike C, Swift allows multiple switch cases to consider the same value or values. In fact, the point (0, 0) could match all four of the cases in this example. However, if multiple matches are possible, the first matching case is always used.

What are the characteristics of switch in Swift?

Swift's switches must be exhaustive, so all possible values of the input must be matched. You can provide a number of cases to match against the value, and also a default case to handle any unmatched values. Another important feature of a Swift switch cases is that they do no automatically 'fallthrough'.

How do switch statements work in Swift?

The switch statement in Swift lets you inspect a value and match it with a number of cases. It's particularly effective for taking concise decisions based on one variable that can contain a number of possible values. Using the switch statement often results in more concise code that's easier to read.

Can switch case have multiple conditions?

The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.


1 Answers

You can use an initial separate pattern matching statement (comparable to a single case independent from the switch statement) that covers the actions that are unique for any (valid) number, and let the switch statement follow with cases that handle actions that are common for several numbers. With this you separate the unique and common logic action, where the latter is simply implemented as usual cases for any pattern matching switch implementation.

E.g., for your example

var myFavoriteNumbers: [Int] = []
var myLeastFavoriteNumbers: [Int] = []
let myNumberDescriptions = ["my number is number 1",
    "this is number 2", "I don't like number 3", "Number Four"]

let myNumber = 1

/* unique action:
    perform a unique action (given a valid number)
    and thereafter proceed to common cases */
if 1...myNumberDescriptions.count ~= myNumber {
    print(myNumberDescriptions[myNumber-1])
}

/* common cases */
switch myNumber {

/* common case: is a favourite number */
case 1...2: myFavoriteNumbers += [myNumber]

/* common case: is a least favourite number */
case 3...4: myLeastFavoriteNumbers += [myNumber]

default:
    print("Default")
}

In case the action that is unique to any number is more complex, use the same approach as above, but use more advanced logic (e.g. an event handler) for the unique action "case".

like image 53
dfrib Avatar answered Sep 20 '22 06:09

dfrib