質問編集履歴

6

コードの追加

2020/07/25 17:07

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- let url:URL = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850")!
81
+ let url:URL = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=")!
82
82
 
83
83
  let task: URLSessionTask = URLSession.shared.dataTask(with: url) { (data, response, error) in
84
84
 

5

コードの追加

2020/07/25 17:07

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
@@ -156,6 +156,136 @@
156
156
 
157
157
  ```
158
158
 
159
+ ### ソースコード2
160
+
161
+ ```ここに言語を入力
162
+
163
+ import UIKit
164
+
165
+
166
+
167
+ class ViewController: UIViewController {
168
+
169
+ //https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850
170
+
171
+
172
+
173
+ @IBOutlet weak var movieTableView: UITableView!
174
+
175
+
176
+
177
+ var movies = [MovieStruct]()
178
+
179
+
180
+
181
+ override func viewDidLoad() {
182
+
183
+ super.viewDidLoad()
184
+
185
+ // Do any additional setup after loading the view.
186
+
187
+
188
+
189
+ fechData()
190
+
191
+ }
192
+
193
+
194
+
195
+ func fechData(){
196
+
197
+
198
+
199
+ let url = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850")!
200
+
201
+ URLSession.shared.dataTask(with: url) { (data, response, error) in
202
+
203
+ guard let data = data else {
204
+
205
+ print(error?.localizedDescription ?? "Unknown error")
206
+
207
+ return
208
+
209
+ }
210
+
211
+
212
+
213
+ let decoder = JSONDecoder()
214
+
215
+
216
+
217
+ if let movies = try? decoder.decode([MovieStruct].self, from: data){
218
+
219
+ DispatchQueue.main.async {
220
+
221
+ self.movies = movies
222
+
223
+ self.movieTableView.reloadData()
224
+
225
+ }
226
+
227
+ }else{
228
+
229
+ print("Unable parse JSON response")
230
+
231
+ }
232
+
233
+ }.resume()
234
+
235
+ }
236
+
237
+ }
238
+
239
+
240
+
241
+ extension ViewController:UITableViewDelegate,UITableViewDataSource{
242
+
243
+
244
+
245
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
246
+
247
+ return movies.count
248
+
249
+ }
250
+
251
+
252
+
253
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
254
+
255
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
256
+
257
+ let movie = movies[indexPath.row]
258
+
259
+
260
+
261
+ cell.textLabel?.text = movie.title
262
+
263
+ cell.textLabel?.text = movie.release_date
264
+
265
+
266
+
267
+ return cell
268
+
269
+ }
270
+
271
+ }
272
+
273
+
274
+
275
+ struct MovieStruct: Codable {
276
+
277
+ var title: String
278
+
279
+ var release_date:String
280
+
281
+ }
282
+
283
+
284
+
285
+
286
+
287
+ ```
288
+
159
289
  ### 問題点
160
290
 
161
291
  上記のコードでappendで格納しているのですが、Cellの数を指定する時にはmoviesではなくMovieStructとしか候補に出てこないので疑問に思いました。

4

コードの変更

2020/07/23 14:19

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
@@ -154,7 +154,7 @@
154
154
 
155
155
  }
156
156
 
157
-
157
+ ```
158
158
 
159
159
  ### 問題点
160
160
 

3

コードの変更

2020/07/23 14:02

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,10 @@
54
54
 
55
55
 
56
56
 
57
+ var movieArray:[MovieStruct] = []
58
+
59
+
60
+
57
61
  // http://www.foxmovies.com/movies
58
62
 
59
63
 
@@ -72,9 +76,7 @@
72
76
 
73
77
  func fechData(){
74
78
 
75
-
76
-
77
- var movieArray:[MovieStruct] = []
79
+
78
80
 
79
81
  let url:URL = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850")!
80
82
 
@@ -94,7 +96,7 @@
94
96
 
95
97
  let movies = try JSONDecoder().decode([MovieStruct].self, from: jsonData)
96
98
 
97
- movieArray.append(contentsOf: movies)
99
+ self.movieArray.append(contentsOf: movies)
98
100
 
99
101
 
100
102
 
@@ -152,111 +154,7 @@
152
154
 
153
155
  }
154
156
 
155
- ```
156
157
 
157
- ### ソースコード2
158
-
159
- ```ここに言語を入力
160
-
161
- import UIKit
162
-
163
-
164
-
165
- var movieArray:[MovieStruct] = []
166
-
167
-
168
-
169
- class ViewController: UIViewController {
170
-
171
- //https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850
172
-
173
-
174
-
175
- override func viewDidLoad() {
176
-
177
- super.viewDidLoad()
178
-
179
- // Do any additional setup after loading the view.
180
-
181
- }
182
-
183
-
184
-
185
- func fechData(){
186
-
187
-
188
-
189
- let url = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850")!
190
-
191
- let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
192
-
193
- guard let data = data else {
194
-
195
- return
196
-
197
- }
198
-
199
- do {
200
-
201
- let movies = try JSONDecoder().decode([MovieStruct].self, from: data)
202
-
203
- movieArray.append(contentsOf: movies)
204
-
205
- } catch{
206
-
207
- print(error.localizedDescription)
208
-
209
- }
210
-
211
- }
212
-
213
- task.resume()
214
-
215
- }
216
-
217
- }
218
-
219
-
220
-
221
- extension ViewController:UITableViewDelegate,UITableViewDataSource{
222
-
223
-
224
-
225
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
226
-
227
- return
228
-
229
- }
230
-
231
-
232
-
233
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
234
-
235
- <#code#>
236
-
237
- }
238
-
239
-
240
-
241
-
242
-
243
- }
244
-
245
-
246
-
247
- struct MovieStruct: Codable {
248
-
249
- var title: String
250
-
251
- var release_date:String
252
-
253
-
254
-
255
- }
256
-
257
-
258
-
259
- ```
260
158
 
261
159
  ### 問題点
262
160
 

2

コードの追加

2020/07/23 14:00

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
@@ -152,7 +152,107 @@
152
152
 
153
153
  }
154
154
 
155
-
155
+ ```
156
+
157
+ ### ソースコード2
158
+
159
+ ```ここに言語を入力
160
+
161
+ import UIKit
162
+
163
+
164
+
165
+ var movieArray:[MovieStruct] = []
166
+
167
+
168
+
169
+ class ViewController: UIViewController {
170
+
171
+ //https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850
172
+
173
+
174
+
175
+ override func viewDidLoad() {
176
+
177
+ super.viewDidLoad()
178
+
179
+ // Do any additional setup after loading the view.
180
+
181
+ }
182
+
183
+
184
+
185
+ func fechData(){
186
+
187
+
188
+
189
+ let url = URL(string: "https://api.themoviedb.org/3/movie/550?api_key=b562d3142fef5c7d35279900383f0850")!
190
+
191
+ let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
192
+
193
+ guard let data = data else {
194
+
195
+ return
196
+
197
+ }
198
+
199
+ do {
200
+
201
+ let movies = try JSONDecoder().decode([MovieStruct].self, from: data)
202
+
203
+ movieArray.append(contentsOf: movies)
204
+
205
+ } catch{
206
+
207
+ print(error.localizedDescription)
208
+
209
+ }
210
+
211
+ }
212
+
213
+ task.resume()
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+ extension ViewController:UITableViewDelegate,UITableViewDataSource{
222
+
223
+
224
+
225
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
226
+
227
+ return
228
+
229
+ }
230
+
231
+
232
+
233
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
234
+
235
+ <#code#>
236
+
237
+ }
238
+
239
+
240
+
241
+
242
+
243
+ }
244
+
245
+
246
+
247
+ struct MovieStruct: Codable {
248
+
249
+ var title: String
250
+
251
+ var release_date:String
252
+
253
+
254
+
255
+ }
156
256
 
157
257
 
158
258
 

1

タグの追加

2020/07/23 13:59

投稿

Ytan
Ytan

スコア39

test CHANGED
File without changes
test CHANGED
File without changes