質問編集履歴
1
質問の内容の簡略化
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
シャッフル処理が分かりません
|
test
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
###
|
1
|
+
###
|
2
|
-
|
3
|
-
5×5の数じのボタンをランダムに配置させたいです。
|
4
2
|
|
5
3
|
|
6
4
|
|
7
|
-
###発生している問題・エラーメッセージ
|
8
5
|
|
9
|
-
1ランダムに配列内ではなっているのにボタンに反映されない
|
10
6
|
|
7
|
+
###
|
8
|
+
|
11
|
-
|
9
|
+
シャッフル関数の意味が分かりません
|
12
10
|
|
13
11
|
```
|
14
12
|
|
@@ -22,23 +20,11 @@
|
|
22
20
|
|
23
21
|
```swift
|
24
22
|
|
25
|
-
ここにご自身が実行したソースコードを書いてください
|
26
|
-
|
27
|
-
```
|
28
|
-
|
29
|
-
import UIKit
|
30
|
-
|
31
|
-
import Foundation
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
extension Array{
|
36
|
-
|
37
|
-
|
23
|
+
mutating func shuffle(){
|
38
24
|
|
39
25
|
for i in 0..<self.count {
|
40
26
|
|
41
|
-
let j = Int(arc4random_uniform(UInt32(self.
|
27
|
+
let j = Int(arc4random_uniform(UInt32(self.count)))
|
42
28
|
|
43
29
|
if i != j {
|
44
30
|
|
@@ -56,111 +42,9 @@
|
|
56
42
|
|
57
43
|
|
58
44
|
|
45
|
+
```
|
59
46
|
|
60
47
|
|
61
|
-
var shuffled:Array {
|
62
|
-
|
63
|
-
var copied = Array<Element>(self)
|
64
|
-
|
65
|
-
copied.shuffle()
|
66
|
-
|
67
|
-
return copied
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
class ViewController: UIViewController {
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
var numButtons = [UIButton]()
|
82
|
-
|
83
|
-
var newArray:[UIButton] = []
|
84
|
-
|
85
|
-
var arrayTitle:[Int] = []
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
override func viewDidLoad() {
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
super.viewDidLoad()
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
let screenWidth = self.view.bounds.width
|
98
|
-
|
99
|
-
let width = screenWidth/5
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
for i in 0 ..< 5 {
|
106
|
-
|
107
|
-
for j in 0 ..< 5 {
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
let button = UIButton()
|
114
|
-
|
115
|
-
button.frame = CGRect(x: width * CGFloat(i), y: 150 + width * CGFloat(j+1), width: width, height: width)
|
116
|
-
|
117
|
-
// button.tag = i * 5 + j
|
118
|
-
|
119
|
-
// button.setTitle("(tag)", for: .normal)
|
120
|
-
|
121
|
-
button.setTitleColor(UIColor.black, for:.normal)
|
122
|
-
|
123
|
-
button.backgroundColor = UIColor(red: 255/255.0, green: 209/255.0, blue: 160/255.0, alpha: 1.0)
|
124
|
-
|
125
|
-
// let title:String = button.currentTitle!
|
126
|
-
|
127
|
-
// let titleCast:Int = Int(title)!
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
button.addTarget(self, action: #selector(tappedButton), for:.touchUpInside)
|
132
|
-
|
133
|
-
numButtons.append(button)
|
134
|
-
|
135
|
-
view.addSubview(button)
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
for btn in numButtons{
|
152
|
-
|
153
|
-
let rdm = arc4random()
|
154
|
-
|
155
|
-
btn.tag = rdm
|
156
|
-
|
157
|
-
let tag = btn.tag
|
158
|
-
|
159
|
-
btn.setTitle("(tag)", for: .normal)
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
}
|
164
48
|
|
165
49
|
|
166
50
|
|
@@ -172,15 +56,7 @@
|
|
172
56
|
|
173
57
|
###試したこと
|
174
58
|
|
175
|
-
課題に対してアプローチしたことを記載してください
|
176
|
-
|
177
|
-
|
59
|
+
関数jにいれた乱数は重複があるはずなのでiとちがった際に値を交換したとしても、配列に重複が生まれる気がするのですが、どうして重複なく、配列の値をシャッフルできたのか教えていただきたいです。
|
178
|
-
|
179
|
-
それがなぜかがわからないです。
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
その後、タイトルをいじろうと思って、乱数を作りタイトルにいれましたが、ランダムな値で配置されるものの、重複した値が並べられてしまいます。
|
184
60
|
|
185
61
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
186
62
|
|