いろいろなパターンを見たいということならば????のはどうでしょう。
可読性も悪く、自分では使いませんが、、、(※落ちる部分は削除しました)
swift
1 func testMethod ( number : Int , tags : [ String ] , products : [ String ] ) -> Int {
2 switch ( number , tags . count ) {
3 case ( 0 , _ ) : return 1
4 case ( _ , 0 ) : return 0
5 case ( 1 ... tags . count , _ ) : return products . isEmpty ? 0 : 1
6 case ( 11 ... 20 , _ ) : return 5
7 default : return 0
8 }
9 }
動きを確認したコード
swift
1 func testMethod ( number : Int , tags : [ String ] , products : [ String ] ) -> Int {
2 if tags . isEmpty {
3 switch number {
4 case 0 :
5 return 1
6 // case 1...tags.count:
7 // return 5
8 default :
9 return 0
10 }
11 } else {
12 switch number {
13 case 0 :
14 return 1
15 case 1 ... tags . count :
16 if products . isEmpty { return 0 }
17 return 1
18 case 11 ... 20 :
19 return 5
20 default :
21 return 0
22 }
23 }
24 }
25
26
27
28 func testMethod1 ( number : Int , tags : [ String ] , products : [ String ] ) -> Int {
29 switch ( number , tags . count ) {
30 case ( 0 , _ ) : return 1
31 case ( _ , 0 ) : return 0
32 case ( 1 ... tags . count , _ ) : return products . isEmpty ? 0 : 1
33 case ( 11 ... 20 , _ ) : return 5
34 default : return 0
35 }
36 }
37
38 print ( testMethod ( number : 0 , tags : [ ] , products : [ ] ) )
39 print ( testMethod1 ( number : 0 , tags : [ ] , products : [ ] ) )
40 print ( "---------" )
41
42 print ( testMethod ( number : 1 , tags : [ ] , products : [ ] ) )
43 print ( testMethod1 ( number : 1 , tags : [ ] , products : [ ] ) )
44 print ( "---------" )
45
46 print ( testMethod ( number : 1 , tags : [ "a" ] , products : [ ] ) )
47 print ( testMethod1 ( number : 1 , tags : [ "a" ] , products : [ ] ) )
48 print ( "---------" )
49
50 print ( testMethod ( number : 1 , tags : [ "a" ] , products : [ "a" ] ) )
51 print ( testMethod1 ( number : 1 , tags : [ "a" ] , products : [ "a" ] ) )
52 print ( "---------" )
53
54 print ( testMethod ( number : 11 , tags : [ "a" , "b" ] , products : [ "a" ] ) )
55 print ( testMethod1 ( number : 11 , tags : [ "a" , "b" ] , products : [ "a" ] ) )
56 print ( "---------" )
57
58 print ( testMethod ( number : 21 , tags : [ "a" , "b" ] , products : [ "a" ] ) )
59 print ( testMethod1 ( number : 21 , tags : [ "a" , "b" ] , products : [ "a" ] ) )
60 print ( "---------" )
61
62