質問編集履歴

15

add code

2020/11/17 14:34

投稿

secretUsed
secretUsed

スコア2

test CHANGED
@@ -1 +1 @@
1
- 複雑なJSON形式でAPIから取得したデータを表示する方法
1
+ 複雑なJSON形式でAPI取得したデータを表示する方法
test CHANGED
File without changes

14

add code

2020/11/17 14:34

投稿

secretUsed
secretUsed

スコア2

test CHANGED
@@ -1 +1 @@
1
- 複雑なJSONからレスポンスを取得する方法
1
+ 複雑なJSON形式でAPIから取得したデータを表示する方法
test CHANGED
File without changes

13

add code

2020/11/17 14:33

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -298,6 +298,8 @@
298
298
 
299
299
 
300
300
 
301
+ //movieIDは他のVIewControllerか取得したものとなります。
302
+
301
303
  URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/(movieID)?api_key=#########&language=ja&append_to_response=credits")!,
302
304
 
303
305
  completionHandler: {data, response, error in

12

add code

2020/11/14 11:48

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -224,11 +224,85 @@
224
224
 
225
225
 
226
226
 
227
+ URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
228
+
229
+ completionHandler: {data, response, error in
230
+
231
+
232
+
233
+ guard let data = data else {
234
+
235
+ print(error?.localizedDescription ?? "Unknown error")
236
+
237
+ return
238
+
239
+ }
240
+
241
+
242
+
243
+ var result: MovieStruct?
244
+
245
+ do {
246
+
247
+ result = try JSONDecoder().decode(MovieStruct.self, from: data)
248
+
249
+ }
250
+
251
+ catch{
252
+
253
+ print("JSON perse error")
254
+
255
+ }
256
+
257
+
258
+
259
+ guard let finalResult = result else {
260
+
261
+ return
262
+
263
+ }
264
+
265
+
266
+
267
+ let newMovies = finalResult.results
268
+
269
+ self.items.append(contentsOf: newMovies)
270
+
271
+ DispatchQueue.main.async {
272
+
273
+ self.table.reloadData()
274
+
275
+ }
276
+
277
+ }).resume()
278
+
279
+ }
280
+
281
+ }
282
+
283
+ ```
284
+
285
+ ### コード案
286
+
287
+ ```
288
+
289
+ private var items:[CastModel] = [CastModel]()
290
+
291
+
292
+
293
+ func fechData(){
294
+
295
+
296
+
297
+ items.removeAll()
298
+
299
+
300
+
227
301
  URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/(movieID)?api_key=#########&language=ja&append_to_response=credits")!,
228
302
 
229
303
  completionHandler: {data, response, error in
230
304
 
231
-
305
+
232
306
 
233
307
  guard let data = data else {
234
308
 
@@ -238,17 +312,17 @@
238
312
 
239
313
  }
240
314
 
241
-
242
-
315
+
316
+
243
- var result: MovieStruct?
317
+ var result: Credits?
244
318
 
245
319
  do {
246
320
 
247
- result = try JSONDecoder().decode(MovieStruct.self, from: data)
321
+ result = try JSONDecoder().decode(Credits.self, from: data)
248
-
322
+
249
- }
323
+ }
250
-
324
+
251
- catch{
325
+ catch {
252
326
 
253
327
  print("JSON perse error")
254
328
 
@@ -262,11 +336,15 @@
262
336
 
263
337
  }
264
338
 
265
-
266
-
339
+
340
+
267
- let newMovies = finalResult.results
341
+ let newMovies = finalResult.cast
268
-
342
+
269
- self.items.append(contentsOf: newMovies)
343
+ self.items.append(contentsOf: newMovies)
344
+
345
+ print(newMovies)
346
+
347
+
270
348
 
271
349
  DispatchQueue.main.async {
272
350
 
@@ -278,82 +356,4 @@
278
356
 
279
357
  }
280
358
 
281
- }
282
-
283
- ```
359
+ ```
284
-
285
- ### コード案
286
-
287
- ```
288
-
289
- private var items:[CastModel] = [CastModel]()
290
-
291
-
292
-
293
- func fechData(){
294
-
295
-
296
-
297
- items.removeAll()
298
-
299
-
300
-
301
- URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
302
-
303
- completionHandler: {data, response, error in
304
-
305
-
306
-
307
- guard let data = data else {
308
-
309
- print(error?.localizedDescription ?? "Unknown error")
310
-
311
- return
312
-
313
- }
314
-
315
-
316
-
317
- var result: Credits?
318
-
319
- do {
320
-
321
- result = try JSONDecoder().decode(Credits.self, from: data)
322
-
323
- }
324
-
325
- catch {
326
-
327
- print("JSON perse error")
328
-
329
- }
330
-
331
-
332
-
333
- guard let finalResult = result else {
334
-
335
- return
336
-
337
- }
338
-
339
-
340
-
341
- let newMovies = finalResult.cast
342
-
343
- self.items.append(contentsOf: newMovies)
344
-
345
- print(newMovies)
346
-
347
-
348
-
349
- DispatchQueue.main.async {
350
-
351
- self.table.reloadData()
352
-
353
- }
354
-
355
- }).resume()
356
-
357
- }
358
-
359
- ```

11

add code

2020/11/14 11:46

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -224,11 +224,85 @@
224
224
 
225
225
 
226
226
 
227
+ URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/(movieID)?api_key=#########&language=ja&append_to_response=credits")!,
228
+
229
+ completionHandler: {data, response, error in
230
+
231
+
232
+
233
+ guard let data = data else {
234
+
235
+ print(error?.localizedDescription ?? "Unknown error")
236
+
237
+ return
238
+
239
+ }
240
+
241
+
242
+
243
+ var result: MovieStruct?
244
+
245
+ do {
246
+
247
+ result = try JSONDecoder().decode(MovieStruct.self, from: data)
248
+
249
+ }
250
+
251
+ catch{
252
+
253
+ print("JSON perse error")
254
+
255
+ }
256
+
257
+
258
+
259
+ guard let finalResult = result else {
260
+
261
+ return
262
+
263
+ }
264
+
265
+
266
+
267
+ let newMovies = finalResult.results
268
+
269
+ self.items.append(contentsOf: newMovies)
270
+
271
+ DispatchQueue.main.async {
272
+
273
+ self.table.reloadData()
274
+
275
+ }
276
+
277
+ }).resume()
278
+
279
+ }
280
+
281
+ }
282
+
283
+ ```
284
+
285
+ ### コード案
286
+
287
+ ```
288
+
289
+ private var items:[CastModel] = [CastModel]()
290
+
291
+
292
+
293
+ func fechData(){
294
+
295
+
296
+
297
+ items.removeAll()
298
+
299
+
300
+
227
301
  URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
228
302
 
229
303
  completionHandler: {data, response, error in
230
304
 
231
-
305
+
232
306
 
233
307
  guard let data = data else {
234
308
 
@@ -238,17 +312,17 @@
238
312
 
239
313
  }
240
314
 
241
-
242
-
315
+
316
+
243
- var result: MovieStruct?
317
+ var result: Credits?
244
318
 
245
319
  do {
246
320
 
247
- result = try JSONDecoder().decode(MovieStruct.self, from: data)
321
+ result = try JSONDecoder().decode(Credits.self, from: data)
248
-
322
+
249
- }
323
+ }
250
-
324
+
251
- catch{
325
+ catch {
252
326
 
253
327
  print("JSON perse error")
254
328
 
@@ -262,11 +336,15 @@
262
336
 
263
337
  }
264
338
 
265
-
266
-
339
+
340
+
267
- let newMovies = finalResult.results
341
+ let newMovies = finalResult.cast
268
-
342
+
269
- self.items.append(contentsOf: newMovies)
343
+ self.items.append(contentsOf: newMovies)
344
+
345
+ print(newMovies)
346
+
347
+
270
348
 
271
349
  DispatchQueue.main.async {
272
350
 
@@ -278,82 +356,4 @@
278
356
 
279
357
  }
280
358
 
281
- }
282
-
283
- ```
359
+ ```
284
-
285
- ### コード案
286
-
287
- ```
288
-
289
- private var items:[CastModel] = [CastModel]()
290
-
291
-
292
-
293
- func fechData(){
294
-
295
-
296
-
297
- items.removeAll()
298
-
299
-
300
-
301
- URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
302
-
303
- completionHandler: {data, response, error in
304
-
305
-
306
-
307
- guard let data = data else {
308
-
309
- print(error?.localizedDescription ?? "Unknown error")
310
-
311
- return
312
-
313
- }
314
-
315
-
316
-
317
- var result: Credits?
318
-
319
- do {
320
-
321
- result = try JSONDecoder().decode(Credits.self, from: data)
322
-
323
- }
324
-
325
- catch {
326
-
327
- print("JSON perse error")
328
-
329
- }
330
-
331
-
332
-
333
- guard let finalResult = result else {
334
-
335
- return
336
-
337
- }
338
-
339
-
340
-
341
- let newMovies = finalResult.cast
342
-
343
- self.items.append(contentsOf: newMovies)
344
-
345
- print(newMovies)
346
-
347
-
348
-
349
- DispatchQueue.main.async {
350
-
351
- self.table.reloadData()
352
-
353
- }
354
-
355
- }).resume()
356
-
357
- }
358
-
359
- ```

10

add code

2020/11/14 11:45

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -286,7 +286,7 @@
286
286
 
287
287
  ```
288
288
 
289
- private var items:[CastItem] = [CastItem]()
289
+ private var items:[CastModel] = [CastModel]()
290
290
 
291
291
 
292
292
 

9

add code

2020/11/14 11:42

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -282,7 +282,39 @@
282
282
 
283
283
  ```
284
284
 
285
+ ### コード案
286
+
287
+ ```
288
+
289
+ private var items:[CastItem] = [CastItem]()
290
+
291
+
292
+
293
+ func fechData(){
294
+
295
+
296
+
297
+ items.removeAll()
298
+
299
+
300
+
301
+ URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
302
+
303
+ completionHandler: {data, response, error in
304
+
305
+
306
+
307
+ guard let data = data else {
308
+
309
+ print(error?.localizedDescription ?? "Unknown error")
310
+
311
+ return
312
+
313
+ }
314
+
315
+
316
+
285
- var result: Credits?
317
+ var result: Credits?
286
318
 
287
319
  do {
288
320
 
@@ -312,68 +344,6 @@
312
344
 
313
345
  print(newMovies)
314
346
 
315
- ### コード案
316
-
317
- ```
318
-
319
- private var items:[CastItem] = [CastItem]()
320
-
321
-
322
-
323
- func fechData(){
324
-
325
-
326
-
327
- items.removeAll()
328
-
329
-
330
-
331
- URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
332
-
333
- completionHandler: {data, response, error in
334
-
335
-
336
-
337
- guard let data = data else {
338
-
339
- print(error?.localizedDescription ?? "Unknown error")
340
-
341
- return
342
-
343
- }
344
-
345
-
346
-
347
- var result: Credits?
348
-
349
- do {
350
-
351
- result = try JSONDecoder().decode(Credits.self, from: data)
352
-
353
- }
354
-
355
- catch {
356
-
357
- print("JSON perse error")
358
-
359
- }
360
-
361
-
362
-
363
- guard let finalResult = result else {
364
-
365
- return
366
-
367
- }
368
-
369
-
370
-
371
- let newMovies = finalResult.cast
372
-
373
- self.items.append(contentsOf: newMovies)
374
-
375
- print(newMovies)
376
-
377
347
 
378
348
 
379
349
  DispatchQueue.main.async {

8

add code

2020/11/14 11:40

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -200,7 +200,7 @@
200
200
 
201
201
  @IBOutlet weak var table: UITableView!
202
202
 
203
- private var items:[CastItem] = [CastItem]()
203
+ private var items:[MovieItem] = [MovieItem]()
204
204
 
205
205
 
206
206
 
@@ -316,7 +316,7 @@
316
316
 
317
317
  ```
318
318
 
319
- private var items:[MovieItem] = [MovieItem]()
319
+ private var items:[CastItem] = [CastItem]()
320
320
 
321
321
 
322
322
 

7

add code

2020/11/14 11:38

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -200,7 +200,7 @@
200
200
 
201
201
  @IBOutlet weak var table: UITableView!
202
202
 
203
- private var items:[MovieItem] = [MovieItem]()
203
+ private var items:[CastItem] = [CastItem]()
204
204
 
205
205
 
206
206
 

6

add code

2020/11/14 11:36

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -282,4 +282,108 @@
282
282
 
283
283
  ```
284
284
 
285
+ var result: Credits?
286
+
287
+ do {
288
+
289
+ result = try JSONDecoder().decode(Credits.self, from: data)
290
+
291
+ }
292
+
293
+ catch {
294
+
295
+ print("JSON perse error")
296
+
297
+ }
298
+
299
+
300
+
301
+ guard let finalResult = result else {
302
+
303
+ return
304
+
305
+ }
306
+
307
+
308
+
309
+ let newMovies = finalResult.cast
310
+
311
+ self.items.append(contentsOf: newMovies)
312
+
313
+ print(newMovies)
314
+
285
315
  ### コード案
316
+
317
+ ```
318
+
319
+ private var items:[MovieItem] = [MovieItem]()
320
+
321
+
322
+
323
+ func fechData(){
324
+
325
+
326
+
327
+ items.removeAll()
328
+
329
+
330
+
331
+ URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
332
+
333
+ completionHandler: {data, response, error in
334
+
335
+
336
+
337
+ guard let data = data else {
338
+
339
+ print(error?.localizedDescription ?? "Unknown error")
340
+
341
+ return
342
+
343
+ }
344
+
345
+
346
+
347
+ var result: Credits?
348
+
349
+ do {
350
+
351
+ result = try JSONDecoder().decode(Credits.self, from: data)
352
+
353
+ }
354
+
355
+ catch {
356
+
357
+ print("JSON perse error")
358
+
359
+ }
360
+
361
+
362
+
363
+ guard let finalResult = result else {
364
+
365
+ return
366
+
367
+ }
368
+
369
+
370
+
371
+ let newMovies = finalResult.cast
372
+
373
+ self.items.append(contentsOf: newMovies)
374
+
375
+ print(newMovies)
376
+
377
+
378
+
379
+ DispatchQueue.main.async {
380
+
381
+ self.table.reloadData()
382
+
383
+ }
384
+
385
+ }).resume()
386
+
387
+ }
388
+
389
+ ```

5

add code

2020/11/14 04:59

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -281,3 +281,5 @@
281
281
  }
282
282
 
283
283
  ```
284
+
285
+ ### コード案

4

add code

2020/11/14 04:50

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -240,15 +240,15 @@
240
240
 
241
241
 
242
242
 
243
- var result: Credits?
243
+ var result: MovieStruct?
244
244
 
245
245
  do {
246
246
 
247
- result = try JSONDecoder().decode(Credits.self, from: data)
247
+ result = try JSONDecoder().decode(MovieStruct.self, from: data)
248
-
248
+
249
- }
249
+ }
250
-
250
+
251
- catch {
251
+ catch{
252
252
 
253
253
  print("JSON perse error")
254
254
 
@@ -262,15 +262,11 @@
262
262
 
263
263
  }
264
264
 
265
-
266
-
265
+
266
+
267
- let newMovies = finalResult.cast
267
+ let newMovies = finalResult.results
268
-
268
+
269
- self.items.append(newMovies)
269
+ self.items.append(contentsOf: newMovies)
270
-
271
- print(newMovies)
272
-
273
-
274
270
 
275
271
  DispatchQueue.main.async {
276
272
 

3

add code

2020/11/14 04:49

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -240,11 +240,11 @@
240
240
 
241
241
 
242
242
 
243
- var result: MovieStruct?
243
+ var result: Credits?
244
244
 
245
245
  do {
246
246
 
247
- result = try JSONDecoder().decode(MovieStruct.self, from: data)
247
+ result = try JSONDecoder().decode(Credits.self, from: data)
248
248
 
249
249
  }
250
250
 
@@ -264,9 +264,9 @@
264
264
 
265
265
 
266
266
 
267
- let newMovies = finalResult.results
267
+ let newMovies = finalResult.cast
268
-
268
+
269
- self.items.append(contentsOf: newMovies)
269
+ self.items.append(newMovies)
270
270
 
271
271
  print(newMovies)
272
272
 

2

add code

2020/11/14 04:47

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -70,7 +70,7 @@
70
70
 
71
71
  // MARK: - Welcome
72
72
 
73
- struct Welcome: Codable {
73
+ struct CastModel: Codable {
74
74
 
75
75
  let id: Int
76
76
 

1

change code

2020/11/14 03:56

投稿

secretUsed
secretUsed

スコア2

test CHANGED
File without changes
test CHANGED
@@ -26,36 +26,32 @@
26
26
 
27
27
  {
28
28
 
29
- "gender": 2,
30
-
31
29
  "id": 6193,
32
30
 
33
- "name": "Leonardo DiCaprio",
31
+ "name": "Leonardo DiCaprio"
32
+
34
-
33
+ }
34
+
35
+ ],
36
+
35
- "order": 0,
37
+ "crew": [
38
+
39
+
40
+
36
-
41
+ {
42
+
43
+ "department": "Directing",
44
+
45
+ "id": 2710,
46
+
47
+ "job": "Director",
48
+
49
+ "name": "James Cameron"
50
+
37
- },
51
+ }
38
52
 
39
53
  ]
40
54
 
41
- "crew": [
42
-
43
- {
44
-
45
- "department": "Directing",
46
-
47
- "gender": 2,
48
-
49
- "id": 2710,
50
-
51
- "job": "Director",
52
-
53
- "name": "James Cameron",
54
-
55
- },
56
-
57
- ]
58
-
59
55
  }
60
56
 
61
57
  }
@@ -72,248 +68,220 @@
72
68
 
73
69
 
74
70
 
71
+ // MARK: - Welcome
72
+
73
+ struct Welcome: Codable {
74
+
75
+ let id: Int
76
+
77
+ let credits: Credits
78
+
79
+ }
80
+
81
+
82
+
83
+ // MARK: - Credits
84
+
85
+ struct Credits: Codable {
86
+
87
+ let cast: [Cast]
88
+
89
+ let crew: [Crew]
90
+
91
+ }
92
+
93
+
94
+
95
+ // MARK: - Cast
96
+
97
+ struct Cast: Codable {
98
+
99
+ let id: Int
100
+
101
+ let name: String
102
+
103
+ }
104
+
105
+
106
+
107
+ // MARK: - Crew
108
+
109
+ struct Crew: Codable {
110
+
111
+ let department: String
112
+
113
+ let id: Int
114
+
115
+ let job, name: String
116
+
117
+ }
118
+
119
+
120
+
121
+ ```
122
+
123
+
124
+
125
+ ### JSON2 & Model2
126
+
127
+ ```
128
+
129
+ //JSON
130
+
131
+ {
132
+
133
+ "results": [
134
+
135
+ {
136
+
137
+ "id": 597,
138
+
139
+ "title": "タイタニック",
140
+
141
+ "release_date": "1997-11-18",
142
+
143
+ "original_language": "en",
144
+
145
+ "original_title": "Titanic",
146
+
147
+ "overview":
148
+
149
+ "poster_path": "/9THZ0bjPRc4qP229dmItJcQTQKU.jpg"
150
+
151
+ },
152
+
153
+ ]
154
+
155
+ }
156
+
157
+ --------------------------------------------------------
158
+
159
+ //Model
160
+
161
+ import Foundation
162
+
163
+
164
+
75
- struct CastItem : Codable {
165
+ struct MovieItem : Codable {
166
+
167
+
76
168
 
77
169
  let id:Int
78
170
 
171
+ let title: String
172
+
173
+ let release_date:String?
174
+
175
+ let poster_path:String?
176
+
177
+ let overview:String?
178
+
179
+ let original_language:String?
180
+
79
- }
181
+ }
80
-
81
-
82
-
182
+
183
+
184
+
83
- struct Credits: Codable {
185
+ struct MovieStruct: Codable {
84
-
186
+
85
- let cast: [Cast]
187
+ let results: [MovieItem]
86
-
87
- let crew: [Crew]
188
+
88
-
89
- }
189
+ }
90
-
91
-
92
-
93
- struct Cast: Codable {
190
+
94
-
95
-
96
-
97
- let gender, id: Int
98
-
99
- let name: String
100
-
101
- let order: Int
191
+ ```
192
+
193
+
194
+
102
-
195
+ ```
103
-
104
-
196
+
105
- enum CodingKeys: String, CodingKey {
197
+ class FirstViewController: UIViewController {
106
-
198
+
199
+
200
+
107
- case gender, id, name, order
201
+ @IBOutlet weak var table: UITableView!
202
+
203
+ private var items:[MovieItem] = [MovieItem]()
204
+
205
+
206
+
207
+ override func viewDidLoad() {
208
+
209
+ super.viewDidLoad()
210
+
211
+
212
+
213
+ fechData()
108
214
 
109
215
  }
110
216
 
111
- }
217
+
112
-
113
-
114
-
218
+
115
- struct Crew: Codable {
219
+ func fechData(){
116
-
117
-
118
-
119
- let department: Department
120
-
121
- let gender, id: Int
122
-
123
- let job, name: String
124
-
125
-
126
-
127
- enum CodingKeys: String, CodingKey {
128
220
 
129
221
 
130
222
 
223
+ items.removeAll()
224
+
225
+
226
+
227
+ URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
228
+
131
- case department, gender, id, job, name
229
+ completionHandler: {data, response, error in
230
+
231
+
232
+
233
+ guard let data = data else {
234
+
235
+ print(error?.localizedDescription ?? "Unknown error")
236
+
237
+ return
238
+
239
+ }
240
+
241
+
242
+
243
+ var result: MovieStruct?
244
+
245
+ do {
246
+
247
+ result = try JSONDecoder().decode(MovieStruct.self, from: data)
248
+
249
+ }
250
+
251
+ catch {
252
+
253
+ print("JSON perse error")
254
+
255
+ }
256
+
257
+
258
+
259
+ guard let finalResult = result else {
260
+
261
+ return
262
+
263
+ }
264
+
265
+
266
+
267
+ let newMovies = finalResult.results
268
+
269
+ self.items.append(contentsOf: newMovies)
270
+
271
+ print(newMovies)
272
+
273
+
274
+
275
+ DispatchQueue.main.async {
276
+
277
+ self.table.reloadData()
278
+
279
+ }
280
+
281
+ }).resume()
132
282
 
133
283
  }
134
284
 
135
285
  }
136
286
 
137
-
138
-
139
- enum Department: String, Codable {
140
-
141
-
142
-
143
- case crew = "Crew"
144
-
145
- case directing = "Directing"
146
-
147
- case production = "Production"
148
-
149
-
150
-
151
- }
152
-
153
- ```
287
+ ```
154
-
155
-
156
-
157
- ### JSON2 & Model2
158
-
159
- ```
160
-
161
- //JSON
162
-
163
- {
164
-
165
- "results": [
166
-
167
- {
168
-
169
- "id": 597,
170
-
171
- "title": "タイタニック",
172
-
173
- "release_date": "1997-11-18",
174
-
175
- "original_language": "en",
176
-
177
- "original_title": "Titanic",
178
-
179
- "overview":
180
-
181
- "poster_path": "/9THZ0bjPRc4qP229dmItJcQTQKU.jpg"
182
-
183
- },
184
-
185
- ]
186
-
187
- }
188
-
189
- --------------------------------------------------------
190
-
191
- //Model
192
-
193
- import Foundation
194
-
195
-
196
-
197
- struct MovieItem : Codable {
198
-
199
-
200
-
201
- let id:Int
202
-
203
- let title: String
204
-
205
- let release_date:String?
206
-
207
- let poster_path:String?
208
-
209
- let overview:String?
210
-
211
- let original_language:String?
212
-
213
- }
214
-
215
-
216
-
217
- struct MovieStruct: Codable {
218
-
219
- let results: [MovieItem]
220
-
221
- }
222
-
223
- ```
224
-
225
-
226
-
227
- ```
228
-
229
- class FirstViewController: UIViewController {
230
-
231
-
232
-
233
- @IBOutlet weak var table: UITableView!
234
-
235
- private var items:[MovieItem] = [MovieItem]()
236
-
237
-
238
-
239
- override func viewDidLoad() {
240
-
241
- super.viewDidLoad()
242
-
243
-
244
-
245
- fechData()
246
-
247
- }
248
-
249
-
250
-
251
- func fechData(){
252
-
253
-
254
-
255
- items.removeAll()
256
-
257
-
258
-
259
- URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=########&language=ja&page=1")!,
260
-
261
- completionHandler: {data, response, error in
262
-
263
-
264
-
265
- guard let data = data else {
266
-
267
- print(error?.localizedDescription ?? "Unknown error")
268
-
269
- return
270
-
271
- }
272
-
273
-
274
-
275
- var result: MovieStruct?
276
-
277
- do {
278
-
279
- result = try JSONDecoder().decode(MovieStruct.self, from: data)
280
-
281
- }
282
-
283
- catch {
284
-
285
- print("JSON perse error")
286
-
287
- }
288
-
289
-
290
-
291
- guard let finalResult = result else {
292
-
293
- return
294
-
295
- }
296
-
297
-
298
-
299
- let newMovies = finalResult.results
300
-
301
- self.items.append(contentsOf: newMovies)
302
-
303
- print(newMovies)
304
-
305
-
306
-
307
- DispatchQueue.main.async {
308
-
309
- self.table.reloadData()
310
-
311
- }
312
-
313
- }).resume()
314
-
315
- }
316
-
317
- }
318
-
319
- ```