Swift - Multiple Switch Cases

Swift - Multiple Switch Cases

how about :

struct Object{
var type = 1
}

let lowType = min(object1.type,object2.type)
let highType = max(object1.type,object2.type)
switch( lowType, highType )
{
case (1,1):
doSome()
case (1,2):
doSome2()
case (1,3):
doSome3()
...
// you will only need to specify the combinations where the first
// type is less or equal to the other.
}

If you will be doing this often, you could define a minMax function to further reduce the amount of code needed each time:

func minMax<T:Comparable>(_ a:T, _ b:T) ->(T,T) 
{ return a<b ? (a,b) : (b,a) }

switch minMax(object1.type,object2.type)
{
case (1,1):
doSome()
case (1,2):
doSome2()
...

note that you can also put each inverted pair in its case statement:

switch(object1.type,object2.type)
{
case (1,1):
doSome()
case (1,2),(2,1):
doSome2()
case (1,3),(3,1):
doSome3()
...
}

[UPDATE]

If you have more than two values to compare in a "permutation insensitive" fashion, you could expand on the minMax() idea by creating variants with more values:

func minToMax<T:Comparable>(_ a:T, _ b:T) -> (T,T) 
{ return a<b ? (a,b) : (b,a) }

func minToMax<T:Comparable>(_ a:T, _ b:T, _ c:T) -> (T,T,T)
{ return { ($0[0],$0[1],$0[2]) }([a,b,c].sorted()) }

func minToMax<T:Comparable>(_ a:T, _ b:T, _ c:T, _ d:T) -> (T,T,T,T)
{ return { ($0[0],$0[1],$0[2],$0[3]) }([a,b,c,d].sorted()) }

switch minToMax(object1.type,object2.type,object3.type)
{
case (1,2,3) : doSome1() // permutations of 1,2,3
case (1,2,4) : doSome2() // permutations of 1,2,4
case (1,2,_) : doSome3() // permutations of 1,2,anything else
case (1,5...6,10) : doSome4() // more elaborate permutations
...
}

}

Swift can a switch case statement have multiple arguments?

Yes, take a look at Swift's documentation on switch statement.

In order to achieve what you want, you need to check for the current value of myLetters:

var myCondition: Bool {
switch self {
case .a, .b: return true
case .c: return false
}
}

Swift: Multiple intervals in single switch-case using tuple

You have to list multiple tuples at the top level:

switch (indexPath.section, indexPath.row) {
case (0, 1...5), (0, 8...10), (0, 30...33):
println("in range")
case (0, _):
println("not at all")
default:
println("wrong section \(indexPath.section)")
}

Managing multiple UIViews names into switch...case Swift statement

As suggested by Joakim comment, the solution is easier than I thought: a simple Array of names, and I post it to further reference. Thank you to the other suggestions, I'll study better.

In the controller scope:

var views = [UIView]()

In viewDidLoad() func:

views = [view0, view1, view2, view3, view4, view5, view6, view7, view8, view9, view10, view0b, view1b, view2b, view3b, view4b, view5b, view6b, view7b, view8b, view9b, view10b]

In the switch...case statement:

        for i in 0...21
{
switch (parsedArray[i]) {
case "0":
views[i].backgroundColor = .systemRed // HERE [i] CALLS SINGLE UIVIEW NAME!
case "1":
views[i].backgroundColor = .systemGreen // HERE [i] CALLS SINGLE UIVIEW NAME!
default:
print("DEFAULT!")
}
}

Can Swift switch statements have another switch in a case?

Of course it's possible

enum Alphabet {
case Alpha, Beta, Gamma
}

enum Disney {
case Goofy, Donald, Mickey
}

let foo : Alphabet = .Beta
let bar : Disney = .Mickey

switch foo {
case .Alpha, .Gamma: break
case .Beta:
switch bar {
case .Goofy, .Donald: break
case .Mickey: print("Mickey")
}
}

How to check multiple Switch case value in Objective-C?

There could be various approaches, depending on exactly what your type1 and type2 data might be.

However, here is a basic example:

NSInteger i = 1;
NSInteger j = 2;

switch (i) {
case 1:
switch (j) {
case 1:
[self functionNormal];
break;

case 2:
[self functionUncommon];
break;

case 3:
[self functionRare];
break;

default:
NSLog(@"first value: 1 ... missing case for second value: for %ld", (long)j);
break;
}
break;

case 2:
switch (j) {
case 1:
[self secondFunctionNormal];
break;

case 2:
[self secondFunctionUncommon];
break;

case 3:
[self secondFunctionRare];
break;

default:
NSLog(@"first value: 2 ... missing case for second value: %ld", (long)j);
break;
}
break;

default:
NSLog(@"missing first case for first value: %ld", (long)i);
break;
}

This is rather inefficient, of course, but maybe it can get you on your way.


Edit

Again, it will depend on your data, but another approach more closely resembling your Swift example:

NSInteger i = 1;
NSInteger j = 2;

NSInteger ij = i * 1000 + j;

switch (ij) {
case 1001:
[self functionNormal];
break;

case 1002:
[self functionUncommon];
break;

case 1003:
[self functionRare];
break;

case 2001:
[self secondFunctionNormal];
break;

case 2002:
[self secondFunctionUncommon];
break;

case 2003:
[self secondFunctionRare];
break;

default:
NSLog(@"case was something else: %ld", (long)ij);
break;
}

Multiple type case in switch

You can use fallthrough:

switch view {
case is UILabel: fallthrough
case is UITextField: print("is label or text field")
default: print("is something else")
}


Related Topics



Leave a reply



Submit