Codable

 

Codableで色々なJSONに対応する - Qiita

 

SwiftでQiitaのAPIを表示させる。 - Qiita

全体的なコードの内容で、画面にtable Viewを追加して、名前をtableViewで紐つけ。

//  private var tableView = UITableView()

コメントアウト

 

実行すると、タイトルとユーザー名を表示できた

 

table Viewのサイズを画面いっぱいにしないようにする場合は、

        //tableView.frame = view.frame

コメントアウト

 

ページを設定

URLQueryItem(name: "page", value: String(displayPage)),

 

var displayPage = 1

グローバルに変更

 

jsonデータを取得するfuncがfetchArticle

class QiitaViewModel {

  static func fetchArticle(completion: @escaping ([QiitaStruct]) -> Swift.Void) {

    let url = "https://qiita.com/api/v2/items"

    guard var urlComponents = URLComponents(string: url) else {

      return

    }

    urlComponents.queryItems = [

      //            URLQueryItem(name: "per_page", value: "20"),

      //          URLQueryItem(name: "page", value: "2"),

      URLQueryItem(name: "page", value: String(displayPage)),

    ]

    let task = URLSession.shared.dataTask(with: urlComponents.url!) { data, response, error in

      guard let jsonData = data else {

        return

      }

      do {

        let articles = try JSONDecoder().decode([QiitaStruct].self, from: jsonData)

        completion(articles)

      } catch {

        print(error.localizedDescription)

      }

    }

    task.resume()

  }

}

 

fetchArticleを実行して、articlesにセット  

    QiitaViewModel.fetchArticle(completion: { (articles) in

//      self.articles = articles

      self.articles = self.articles + articles

      DispatchQueue.main.async {

        self.table.reloadData()

        self.isLoading = false

      }

    })

  

Swift4 Codableがすごすぎた件 - Qiita

 

Codableについて色々まとめた[Swift4.x] - Qiita