gpt4 book ai didi

ios - 在 swift 4 中处理 JSON

转载 作者:行者123 更新时间:2023-11-30 11:02:42 24 4
gpt4 key购买 nike

我有这个 JSON 数据

{  
"siteInfo":{
"siteName":"Name",
"logo":"Some String",
"snappchat":"Some String",
"whatsapp":"Some String",
"twitter":"Some String",
"groupName":"Some String"
},
"Apps":[
{
"category":Some String,
"app_name":"Some String",
"description":"Some String",
"version":"Some String",
"icon":"Some String",
"download_counter":0,
"appUrl":"Some String",
"size":"Some String"
},
{
"category":Some String,
"app_name":"Some String",
"description":"Some String",
"version":"Some String",
"icon":"Some String",
"download_counter":0,
"appUrl":"Some String",
"size":"Some String"
}
],
"notes":null
}

我使用此代码下载它:

var app = [Apps]()

func DownloadFromStore(url:String) {
DispatchQueue.global().sync {
guard let url = URL(string: url) else { return }

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
print(error?.localizedDescription ?? "Response Error")
return
}

do {
let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: [])
print(jsonResponse)

print("hi?")
guard let jsonArray = jsonResponse as? [[String: Any]] else {
return
}
print(jsonArray)

for dic in jsonArray {
guard let title = dic["Apps"] as? String else { return }
print(title) // Output
}
} catch let parsingError {
print("Error", parsingError)
}
}.resume()
}
}

这是我的结构:

class API : Codable {
let siteInfo : siteInfo?
let Apps : [Apps]?
let notes : String?
init(Apps: [Apps] ,siteInfo: siteInfo , notes : String)
{
self.Apps = Apps
self.siteInfo = siteInfo
self.notes = notes
}
}

class siteInfo : Codable
{
let siteName : String?
let logo : String?
let snappchat : String?
let whatsapp : String?
let twitter : String?
let groupName : String?

init(siteName : String , logo:String , snappchat :String , whatsapp: String, twitter :String ,groupName:String)
{
self.siteName = siteName
self.logo = logo
self.snappchat = snappchat
self.whatsapp = whatsapp
self.twitter = twitter
self.groupName = groupName
}
}

class Apps: Codable {
let appUrl : String
let app_name : String
let category :String
let description : String
let download_counter : Int
let icon : String
let size : String
let version : String

init(appUrl : String ,app_name : String , category : String , description : String ,download_counter : Int , icon :String , size : String, version : String )
{
self.appUrl = appUrl
self.app_name = app_name
self.category = category
self.description = description
self.download_counter = download_counter
self.icon = icon
self.size = size
self.version = version
}
}

这是我的collectionView代码:

let Applists = [Apps]()

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

let appfinal = Applists[indexPath.row]

cell.appnamelabel.text = appfinal.app_name

cell.appsizelabel.text = appfinal.size
cell.appverlabel.text = appfinal.version
cell.SetImage(url: appfinal.icon ?? "")

return cell
}

为什么当Applists.count的值是0并且我的数组中没有任何内容时?

collectionView 也是空的。

但是当我在 func Download() 中打印时,它给了我我想要的一切,而且非常完美。

每个代码都位于不同的文件中。

最佳答案

在 API 响应中完成创建模型后,重新加载 Collection View 数据。

关于ios - 在 swift 4 中处理 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133838/

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