0

我正在用 Swift 开发一个爱好项目 iOS 应用程序,以学习在 Moya 中使用 JSON。这是我的 VC 代码的样子:

import UIKit
import Moya
import Moya_ModelMapper

class SQSquirrelListViewController: UIViewController {

  @IBOutlet weak var tableView: UITableView!

  var squirrels: [Squirrel] = []

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.fetchData()
  }


  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
  }

  func fetchData() {
    let provider = MoyaProvider<SQService>()
    provider.request(.squirrelsIndex(page: "1")) { result in
      switch result {
      case let .success(moyaResponse):
        do {
          let sq = try moyaResponse.mapObject() as Squirrels
          self.squirrels = sq.items
        } catch {
          print(error.localizedDescription)
        }
        let statusCode = moyaResponse.statusCode
        print("STATUS CODE: \(statusCode)")
      case let .failure(error):
        print(error.localizedDescription)
      }
    }
    tableView.reloadData()
  }
}

启动应用程序后,squirrels数组为空。我不确定它是否与 Moya 有关,还是我的 View Controller 流程​​中的缺陷?

4

1 回答 1

2

重新加载表视图的行必须完成处理程序内。

于 2017-02-01T17:14:18.437 回答