gpt4 book ai didi

json - 打开应用程序时如何加载数据?

转载 作者:搜寻专家 更新时间:2023-11-01 06:53:20 24 4
gpt4 key购买 nike

我写的代码是正确的,但由于某些原因,在调用fetchData() 方法后,superHero 数组仍然为空。我做错了什么?

class MainViewController: UICollectionViewController {

private var superHeroes = [SuperHero]()

let url = "https://cdn.rawgit.com/akabab/superhero-api/0.2.0/api/all.json"

override func viewDidLoad() {
super.viewDidLoad()

fetchData()
}

// MARK: UICollectionViewDataSource

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return superHeroes.count
}

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

let superHeroe = superHeroes[indexPath.row]
cell.configureCell(superHero: superHeroe)


return cell
}

func fetchData() {

guard let url = URL(string: url) else { return }

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

guard let data = data else { return }

do {
self.superHeroes = try JSONDecoder().decode([SuperHero].self, from: data)
print(self.superHeroes)
} catch let error {
print(error)
}
}.resume()
}
}

最佳答案

因为你还没有发布你的SuperHero Codable 我根据你的JSONHERE 创建了一个新的.它看起来像:

typealias SuperHero = [SuperHeroElement]

struct SuperHeroElement: Codable {
let id: Int
let name, slug: String
let powerstats: Powerstats
let appearance: Appearance
let biography: Biography
let work: Work
let connections: Connections
let images: Images
}

struct Appearance: Codable {
let gender: Gender
let race: String?
let height, weight: [String]
let eyeColor, hairColor: String
}

enum Gender: String, Codable {
case empty = "-"
case female = "Female"
case male = "Male"
}

struct Biography: Codable {
let fullName, alterEgos: String
let aliases: [String]
let placeOfBirth, firstAppearance: String
let publisher: String?
let alignment: Alignment
}

enum Alignment: String, Codable {
case bad = "bad"
case empty = "-"
case good = "good"
case neutral = "neutral"
}

struct Connections: Codable {
let groupAffiliation, relatives: String
}

struct Images: Codable {
let xs, sm, md, lg: String
}

struct Powerstats: Codable {
let intelligence, strength, speed, durability: Int
let power, combat: Int
}

struct Work: Codable {
let occupation, base: String
}

接下来您需要更换:

private var superHeroes = [SuperHero]()

private var superHeroes: SuperHero?

并在您的 fetchData 方法中替换

self.superHeroes = try JSONDecoder().decode([SuperHero].self, from: data)

self.superHeroes = try JSONDecoder().decode(SuperHero.self, from: data)

这里的 superHeroesSuperHeroElementArray,因此您可以通过索引从中访问数据。喜欢

guard let response = self.superHeroes else {return}
print(response[0].images) //Accessing data at 0 index

一旦您从主队列中的服务器获取数据,您还需要重新加载您的 Collection View ,例如:

DispatchQueue.main.async {
self.collectionView.reloadData()
}

关于json - 打开应用程序时如何加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com