質問編集履歴
1
エンコードしたコードをのせました
test
CHANGED
File without changes
|
test
CHANGED
@@ -368,6 +368,146 @@
|
|
368
368
|
|
369
369
|
|
370
370
|
|
371
|
+
###urlエンコードしたコード
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
```swift
|
376
|
+
|
377
|
+
import UIKit
|
378
|
+
|
379
|
+
import Accounts
|
380
|
+
|
381
|
+
import SwiftyJSON
|
382
|
+
|
383
|
+
import Alamofire
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
class ViewController: UIViewController, UITableViewDataSource {
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
var articles: [[String: String?]] = []
|
392
|
+
|
393
|
+
let table = UITableView()
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
override func viewDidLoad() {
|
398
|
+
|
399
|
+
super.viewDidLoad()
|
400
|
+
|
401
|
+
title = "記事一覧"
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
table.frame = view.frame
|
406
|
+
|
407
|
+
view.addSubview(table)
|
408
|
+
|
409
|
+
table.dataSource = self
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
getArticles()
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
func getArticles() {
|
420
|
+
|
421
|
+
let appid = "AIzaSyCAIznRo4MUdgBtijMnd2De9YEUqMdmwaY"
|
422
|
+
|
423
|
+
let entryUrl: String = "https://www.googleapis.com/youtube/v3/search?key=AIzaSyCAIznRo4MUdgBtijMnd2De9YEUqMdmwaY&q="
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
let inputText = entryUrl + "へぼてっく" + "&part=snippet&maxResults=10&order=viewCount"
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
guard let escapedInputText = inputText.addingPercentEncoding(
|
432
|
+
|
433
|
+
withAllowedCharacters: CharacterSet.urlQueryAllowed
|
434
|
+
|
435
|
+
) else {
|
436
|
+
|
437
|
+
return
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
Alamofire.request(escapedInputText)
|
444
|
+
|
445
|
+
.responseJSON { response in
|
446
|
+
|
447
|
+
guard let object = response.result.value else {
|
448
|
+
|
449
|
+
return
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
let json = JSON(object)
|
456
|
+
|
457
|
+
print(json)
|
458
|
+
|
459
|
+
json.forEach { (_, json) in
|
460
|
+
|
461
|
+
let article: [String: String?] = [
|
462
|
+
|
463
|
+
"title": json["items"]["snippet"]["title"].string,
|
464
|
+
|
465
|
+
"userId": json["items"]["id"]["videoId"].string,
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
]
|
470
|
+
|
471
|
+
self.articles.append(article)
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
self.table.reloadData()
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
484
|
+
|
485
|
+
return articles.count
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
492
|
+
|
493
|
+
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
|
494
|
+
|
495
|
+
let article = articles[indexPath.row]
|
496
|
+
|
497
|
+
cell.textLabel?.text = article["title"]!
|
498
|
+
|
499
|
+
cell.detailTextLabel?.text = article["userId"]!
|
500
|
+
|
501
|
+
return cell
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
```
|
508
|
+
|
509
|
+
|
510
|
+
|
371
511
|
### 補足情報(FW/ツールのバージョンなど)
|
372
512
|
|
373
513
|
|