質問編集履歴
5
a
test
CHANGED
File without changes
|
test
CHANGED
@@ -178,59 +178,167 @@
|
|
178
178
|
|
179
179
|
|
180
180
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
181
|
+
## SearchRootVC全文
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```SearchRootVC
|
186
|
+
|
187
|
+
import UIKit
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
class SearchRootVC: UITableViewController, UISearchBarDelegate {
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
@IBOutlet weak var searchBar: UISearchBar!
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
// Var
|
200
|
+
|
201
|
+
var word: String!
|
202
|
+
|
203
|
+
var url: String!
|
204
|
+
|
205
|
+
var RepoToPass: Int!
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
var task: URLSessionTask?
|
210
|
+
|
211
|
+
var repo: [[String: Any]]=[]
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
var repositoryApi = RepositoryApi()
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
override func viewDidLoad() {
|
220
|
+
|
221
|
+
super.viewDidLoad()
|
222
|
+
|
223
|
+
setupTableView()
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
// 以下3つ、元から用意されているsearchBar関数名なので、変更NG。
|
230
|
+
|
231
|
+
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
|
232
|
+
|
233
|
+
searchBar.text = ""
|
234
|
+
|
235
|
+
return true
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
242
|
+
|
243
|
+
task?.cancel()
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
250
|
+
|
251
|
+
repositoryApi.getRandomRepoUrlSession()
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
func setupTableView() {
|
258
|
+
|
259
|
+
// UISearchBarのdelegateプロパティに、self(=SearchRootVC)を代入。
|
260
|
+
|
261
|
+
searchBar.delegate = self
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
268
|
+
|
269
|
+
if segue.identifier == Segues.ToProfileDetail {
|
270
|
+
|
271
|
+
if let detailVC = segue.destination as? ProfileDetailVC {
|
272
|
+
|
273
|
+
detailVC.selectedUser = self
|
230
274
|
|
231
275
|
}
|
232
276
|
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
// extension
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
extension SearchRootVC {
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
296
|
+
|
297
|
+
return repo.count
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
// dequeueReusableCellで、セルを再利用。
|
308
|
+
|
309
|
+
// nilを返さない為、オプショナルバインディングは不要。
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
let cell: RepositoryCell = tableView.dequeueReusableCell(withIdentifier: Identifiers.RepositoryCell, for: indexPath) as! RepositoryCell
|
314
|
+
|
315
|
+
let UserRepo = repo[indexPath.row]
|
316
|
+
|
317
|
+
cell.configureCell(UserRepo)
|
318
|
+
|
319
|
+
cell.tag = indexPath.row
|
320
|
+
|
321
|
+
return cell
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
328
|
+
|
329
|
+
RepoToPass = indexPath.row
|
330
|
+
|
331
|
+
performSegue(withIdentifier: Segues.ToProfileDetail, sender: self)
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
233
|
-
```
|
339
|
+
```
|
340
|
+
|
341
|
+
|
234
342
|
|
235
343
|
|
236
344
|
|
4
あ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
self
|
1
|
+
do-catch内にてselfエラー【URLSessionを他クラスから呼び出したい。】
|
test
CHANGED
File without changes
|
3
あ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
selfが原因の、EXC_BAD_ACCESSが出る【URLSessionを他クラスから呼び出したい。】
|
test
CHANGED
@@ -1,31 +1,217 @@
|
|
1
|
-
##
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
## やりたいこと
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
`SearchRootVC`クラスにて、**URLSession**のコードを`RepositoryApi`クラスから呼び出したい。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```SearchRootVC
|
10
|
+
|
11
|
+
var repositoryApi = RepositoryApi()
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
...
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
20
|
+
|
21
|
+
repositoryApi.getRandomRepoUrlSession()
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```RepositoryApi
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
// URLSessionの処理
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
## selfが原因の、EXC_BAD_ACCESSが出る
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
`EXC_BAD_ACCESS`エラーにて、
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
`self = CodeCheck_Test_Yumemi.SearchRootVC 0x00007ff8c083db00`だと エラー原因が特定出来たので、
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
下記を記述して`self`の使用を避けようとしましたが、引き続き`EXC_BAD_ACCESS`でクラッシュします...
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```RepositoryApi
|
56
|
+
|
57
|
+
var searchRootVC = SearchRootVC()
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
## URLSessionコード
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
なお、呼び出しをせずに`SearchRootVC`のみで実行すると
|
70
|
+
|
71
|
+
`EXC_BAD_ACCESS`は無く、正常に動作します。
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```RepositoryApi
|
76
|
+
|
77
|
+
import UIKit
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
class RepositoryApi: SearchRootVC {
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
var searchRootVC = SearchRootVC()
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
func getRandomRepoUrlSession() {
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
let word = searchBar.text!
|
96
|
+
|
97
|
+
let REPOSITORY_URL = URL_BASE + "(word)"
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// nilは許さない。urlの強制アンラップを修正。
|
102
|
+
|
103
|
+
guard let url = URL(string: REPOSITORY_URL) else { return }
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
let task = URLSession.shared.dataTask(with: url) { (data, responce, error) in
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
guard error == nil else {
|
112
|
+
|
113
|
+
debugPrint(error.debugDescription)
|
114
|
+
|
115
|
+
return
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// dataの強制アンラップを修正。
|
122
|
+
|
123
|
+
guard let data = data else { return }
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// try!は、例外が発生したときにはクラッシュするので修正。(-> エラーが起こり得ないケースでのみ使用可)
|
128
|
+
|
129
|
+
// try?で例外を安全に無視できるが、エラーを表示するため do-catch を使用。
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
do {
|
134
|
+
|
135
|
+
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
136
|
+
|
137
|
+
if let items = json?["items"] as? [[String: Any]] {
|
138
|
+
|
139
|
+
self.searchRootVC.repo = items
|
140
|
+
|
141
|
+
// DispatchQueue で一つ以上のタスクを管理し、async で複数のAPIの非同期通信を実行。
|
142
|
+
|
143
|
+
DispatchQueue.main.async {
|
144
|
+
|
145
|
+
// UIを更新する処理
|
146
|
+
|
147
|
+
self.searchRootVC.tableView.reloadData()
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
} catch {
|
154
|
+
|
155
|
+
debugPrint(error.localizedDescription)
|
156
|
+
|
157
|
+
return
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
// 新しく初期化されたタスクは一時停止状態で開始されるため、このメソッドを呼び出してタスクを開始する必要がある。
|
164
|
+
|
165
|
+
task.resume()
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
## 追記
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
`let json`は、`guard let json`とすべきなのでしょうか?
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
// dataの強制アンラップを修正。
|
194
|
+
|
195
|
+
guard let data = data else { return }
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
// try!は、例外が発生したときにはクラッシュするので修正。(-> エラーが起こり得ないケースでのみ使用可)
|
200
|
+
|
201
|
+
// try?で例外を安全に無視できるが、エラーを表示するため do-catch を使用。
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
do {
|
206
|
+
|
207
|
+
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
208
|
+
|
209
|
+
if let items = json?["items"] as? [[String: Any]] {
|
26
210
|
|
27
211
|
self.repo = items
|
28
212
|
|
213
|
+
// DispatchQueue で一つ以上のタスクを管理し、async で複数のAPIの非同期通信を実行。
|
214
|
+
|
29
215
|
DispatchQueue.main.async {
|
30
216
|
|
31
217
|
// UIを更新する処理
|
@@ -36,54 +222,6 @@
|
|
36
222
|
|
37
223
|
}
|
38
224
|
|
39
|
-
}
|
40
|
-
|
41
|
-
```
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
####2
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
`let json`は、`guard let json`とすべきなのでしょうか?
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
```
|
54
|
-
|
55
|
-
// dataの強制アンラップを修正。
|
56
|
-
|
57
|
-
guard let data = data else { return }
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
// try!は、例外が発生したときにはクラッシュするので修正。(-> エラーが起こり得ないケースでのみ使用可)
|
62
|
-
|
63
|
-
// try?で例外を安全に無視できるが、エラーを表示するため do-catch を使用。
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
do {
|
68
|
-
|
69
|
-
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
70
|
-
|
71
|
-
if let items = json?["items"] as? [[String: Any]] {
|
72
|
-
|
73
|
-
self.repo = items
|
74
|
-
|
75
|
-
// DispatchQueue で一つ以上のタスクを管理し、async で複数のAPIの非同期通信を実行。
|
76
|
-
|
77
|
-
DispatchQueue.main.async {
|
78
|
-
|
79
|
-
// UIを更新する処理
|
80
|
-
|
81
|
-
self.tableView.reloadData()
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
225
|
} catch {
|
88
226
|
|
89
227
|
debugPrint(error.localizedDescription)
|
@@ -96,8 +234,6 @@
|
|
96
234
|
|
97
235
|
|
98
236
|
|
99
|
-
|
237
|
+
質問は以上です。
|
100
|
-
|
101
|
-
|
102
238
|
|
103
239
|
お時間あるときに、ご返信頂けましたら幸いです????
|
2
あ
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
## JSONコード
|
1
|
+
## リファクタリングしたJSONコードの添削。
|
2
|
-
|
3
|
-
|
4
2
|
|
5
3
|
強制アンラップ、不必要なIUOを考慮してリファクタリングしたコードが2です。
|
6
4
|
|
1
a
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
JSONコードにて、guard let jsonとすべきなのか。
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
##
|
1
|
+
## JSONコードにて、guard let jsonとすべきなのか。
|
2
2
|
|
3
3
|
|
4
4
|
|