質問編集履歴

1

追記(ご指摘に対する対応)

2016/03/01 15:06

投稿

hi_ro_kun
hi_ro_kun

スコア7

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,37 @@
17
17
  func test(gotit: Bool) -> [Int] { return gotit ? [[1,2,3],[4,5,6]] : [1,2,3,4,5,6] }
18
18
 
19
19
  ```
20
+
21
+
22
+
23
+ 追記(ご指摘に対する対応)
24
+
25
+ 詳細を説明せず、大変失礼いたしました。 実は、C++の得意な友人より、多次元配列の宣言が面倒な言語は 使う気になれないとバカにされ、何とか関数実装できないかと考え、 以下のような関数を作成したのですが、同じ関数で違う次元の 配列を返すことができればと思い相談させていただきました。
26
+
27
+
28
+
29
+ ```Swift
30
+
31
+ func Array1<T>(index: [Int], rV: T) -> [T] { return [T](count: index[index.count-1], repeatedValue: rV) }
32
+
33
+ func Array2<T>(index: [Int], rV: T) -> [[T]] { return [[T]](count: index[index.count-2], repeatedValue: Array1(index, rV: rV)) }
34
+
35
+ func Array3<T>(index: [Int], rV: T) -> [[[T]]] { return [[[T]]](count: index[index.count-3], repeatedValue: Array2(index, rV: rV)) }
36
+
37
+
38
+
39
+ var sw: [[[Bool]]] = Array3([2,3,2], rV: true)
40
+
41
+ print(sw)
42
+
43
+ // [[[true, true], [true, true], [true, true]], [[true, true], [true, true], [true, true]]]
44
+
45
+
46
+
47
+ var dbl: [[[Double]]] = Array3([2,2,3], rV: Double(0.0))
48
+
49
+ print(dbl)
50
+
51
+ // [[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]
52
+
53
+ ```