从'didset'JSON解码的数组数据返回的Nil值

时间:2017-11-24 03:23:56

标签: ios json swift

我似乎无法在下面设置电视节目和缩略图。我正确生成数据数组,因为print(shows)语句正确打印所有项目。任何人都可以看到我出错的地方。没有错误,但我的观点是空的,没有图像或标题。 这是TvController类

class TvController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var tvShows: [RecentTvList]? = []


func fetchTvItems()   {

    let jsonUrlString = "https://www.what-song.com/api/recent-tv?page=1"
    guard let url = URL(string: jsonUrlString) else
    {return}

    URLSession.shared.dataTask(with: url) { (data, response, err) in

        guard let data = data else {return}

        do {

            let shows =  try
                JSONDecoder().decode(RecentTvListData.self, from: data)
            print(shows)

            self.tvShows = shows.data

        } catch let jsonErr {
            print("Error serializing JSON", jsonErr)
        }

    }.resume()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchTvItems()

    navigationItem.title = "TV"
    navigationController?.navigationBar.isTranslucent = false

    collectionView?.backgroundColor = .white

    collectionView?.register(TvCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(Header.self, forSupplementaryViewOfKind:
        UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if let count = tvShows?.count    {
        return count
    }

    return 0

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TvCell

    cell.televisionShow = tvShows?[indexPath.item]

    return cell
}
}

这是模型文件:

struct RecentTvListData: Decodable  {
var data: [RecentTvList]
}

struct RecentTvList: Decodable  {
var title: String?
var poster_url: String?
}

以下是单元格的视图:所有视图/子视图/约束都已正确设置,我只是不打扰包含代码,因为它使所有这些都过于膨胀。我非常确定问题出在模型或控制器上。

var tvShowsController: TvController?


var televisionShow: RecentTvList? {
    didSet {
        titleLabel.text = televisionShow?.title
        setupThumbnailImage()
        thumbnailImageView.image = UIImage(named: (televisionShow?.poster_url)!)
    }
}

let thumbnailImageView: UIImageView = {
    let imageView = UIImageView(image: #imageLiteral(resourceName: "stranger things poster"))
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 6
    imageView.contentMode = .scaleAspectFill
    imageView.layer.masksToBounds = true
    return imageView
}()

let titleLabel: UILabel = {
    let title = UILabel()
    title.text = "Stranger Things"
    title.font = UIFont(name: "Montserrat", size: 17)
    title.translatesAutoresizingMaskIntoConstraints = false
    return title
}()

1 个答案:

答案 0 :(得分:1)

下载数据(即URLSession.shared.dataTask(with: url)之后的块中的所有代码)是一个异步操作,这意味着iOS在不同的线程上运行该代码,在该线程上它需要尽可能多的时间来完成并立即执行继续执行当前(主要,在这种情况下)线程。当您的收藏视图加载并尝试显示其数据时,它尚未完成下载,因此无法显示数据。您可以通过调用reloadData强制您的收藏视图在完成下载后加载数据,如下所示:

URLSession.shared.dataTask(with: url) { (data, response, err) in

    guard let data = data else {return}

    do {

        let shows =  try
            JSONDecoder().decode(RecentTvListData.self, from: data)
        print(shows)

        self.tvShows = shows.data
        self.collectionView?.reloadData()

    } catch let jsonErr {
        print("Error serializing JSON", jsonErr)
    }

}.resume()