gpt4 book ai didi

swift - 如何使用 swift 处理 JSON 中丢失的对象?

转载 作者:行者123 更新时间:2023-11-28 05:45:22 26 4
gpt4 key购买 nike

我目前正在从 Tracker Network Apex Legends Api 获取数据。我遇到了一个问题,当我使用...

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

将抛出一个错误。据我所知,这是因为某些“ child ”没有值(value)观。

简单来说,我想知道如何使用上面的代码并将任何空值或缺失值替换为字符串,例如数组中的“0”或“nil”?

我将这些值放入一个数组中,然后将它们显示在 TableView 中。

下面的代码工作得很好,因为每个实例都有值。

self.title2 = json.data.children.map{$0.stats[0].metadata.name}

这是我正在使用的以下代码。

 import Foundation

struct Store: Codable {
let data: DataClass

enum CodingKeys: String, CodingKey {
case data = "data"
}
}

struct DataClass: Codable {
let id: String
let type: String
let children: [Child]
let metadata: DataMetadata
let stats: [Stat]

enum CodingKeys: String, CodingKey {
case id = "id"
case type = "type"
case children = "children"
case metadata = "metadata"
case stats = "stats"
}
}

struct Child: Codable {
let id: String
let type: String
let metadata: ChildMetadata
let stats: [Stat]

enum CodingKeys: String, CodingKey {
case id = "id"
case type = "type"
case metadata = "metadata"
case stats = "stats"
}
}

struct ChildMetadata: Codable {
let legendName: String
let icon: String
let bgimage: String

enum CodingKeys: String, CodingKey {
case legendName = "legend_name"
case icon = "icon"
case bgimage = "bgimage"
}
}

struct Stat: Codable {
let metadata: StatMetadata
let value: Int
let percentile: Double?
let rank: Int?
let displayValue: String
let displayRank: String

enum CodingKeys: String, CodingKey {
case metadata = "metadata"
case value = "value"
case percentile = "percentile"
case rank = "rank"
case displayValue = "displayValue"
case displayRank = "displayRank"
}
}

struct StatMetadata: Codable {
let key: String
let name: String
let categoryKey: StatsCategoryOrder
let categoryName: CategoryName
let isReversed: Bool

enum CodingKeys: String, CodingKey {
case key = "key"
case name = "name"
case categoryKey = "categoryKey"
case categoryName = "categoryName"
case isReversed = "isReversed"
}
}

enum StatsCategoryOrder: String, Codable {
case combat = "combat"
case game = "game"
case weapons = "weapons"
}

enum CategoryName: String, Codable {
case combat = "Combat"
case game = "Game"
case weapons = "Weapons"
}

struct DataMetadata: Codable {
let statsCategoryOrder: [StatsCategoryOrder]
let platformID: Int
let platformUserHandle: String
let accountID: String
let cacheExpireDate: String
let level: Int

enum CodingKeys: String, CodingKey {
case statsCategoryOrder = "statsCategoryOrder"
case platformID = "platformId"
case platformUserHandle = "platformUserHandle"
case accountID = "accountId"
case cacheExpireDate = "cacheExpireDate"
case level = "level"
}
}

用于获取 json 数据并放入数组。

//images
var legendImgArr = [""]
//legend name
var legendTitle = [""]
//data set 1
var title1 = [""]
var data1 = [0]
//data set 2
var title2 = [""]
var data2 = [0]
//data set 3
var title3 = ["nil"]
var data3 = [0]

加载到 UITableView。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! statsOutlets

//loading Legend Images
let request = NSURLRequest(url: NSURL(string: legendImgArr[indexPath.row])! as URL)

URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in

if error != nil {
print("Failed to load image for url: \(self.legendImgArr), error: ")
return
}

guard let httpResponse = response as? HTTPURLResponse else {
print("Not an NSHTTPURLResponse from loading url: \(self.legendImgArr)")
return
}

if httpResponse.statusCode != 200 {
print("Bad response statusCode: \(httpResponse.statusCode) while loading url: \(self.legendImgArr)")
return
}

DispatchQueue.main.asyncAfter(deadline: .now() ) { () -> Void in
cell.legendImage.image = UIImage(data: data!)
}

}.resume()

//loading labels
cell.legendName.text = legendTitle[indexPath.row]


cell.stat1Title.text = title1[indexPath.row]
cell.stat1Data.text = String(data1[indexPath.row])

cell.stat2Title.text = title2[indexPath.row]
cell.stat2Data.text = String(data2[indexPath.row])

cell.stat3Title.text = title3[indexPath.row]
cell.stat3Data.text = String(data3[indexPath.row])

return cell
}

获取Json数据。

let formattedName = usernameEntry.text!.replacingOccurrences(of: " ", with: "%20")

let PlayerStatURL = URL(string: "https://public-api.tracker.gg/apex/v1/standard/profile/\(selectedPlatform)/\(formattedName)")

if let unwrappedURL = PlayerStatURL {
var request = URLRequest(url: unwrappedURL)
request.addValue("my-api-key", forHTTPHeaderField: "TRN-Api-Key")
let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
// you should put in error handling code, too
if let data = data {
do {
let json = try JSONDecoder().decode(Store.self, from: data) as Store

// print(json.data)


//account level
print("Level: \(json.data.metadata.level)")

//images
self.legendImgArr = json.data.children.map{$0.metadata.icon}
//titles
self.legendTitle = json.data.children.map{$0.metadata.legendName}
//label 1 (this label works fine due to there being values for all 'legendName')
self.title1 = json.data.children.map{$0.stats[0].metadata.name}
self.data1 = json.data.children.map{$0.stats[0].value}


//!!!Error happens here (I think this is because there are null values or nothing at all for some of them)!!!
self.title2 = json.data.children.map{$0.stats[1].metadata.name}
self.data2 = json.data.children.map{$0.stats[1].value}
self.title3 = json.data.children.map{$0.stats[2].metadata.name}
self.data3 = json.data.children.map{$0.stats[2].value}

} catch {
print(error.localizedDescription)

}
DispatchQueue.main.asyncAfter(deadline: .now() ) {
self.tableView.reloadData()
self.viewDidLoad()
self.statusOfLoad.stopAnimating()
self.statusOfLoad.isHidden = true
}
}

}
dataTask.resume()


}


}

我已经把错误发生的地方放在上面了。还知道这有效...

self.title2 = json.data.children[0].stats[1].metadata.name

但这行不通

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

这是有效代码的输出。

self.title1 = json.data.children.map{$0.stats[0].metadata.name}

输出:

["Kills", "Kills", "Kills", "Kills", "Kills", "Kills", "Kills", "Kills"]

这是无效代码的输出。

self.title2 = json.data.children.map{$0.stats[1].metadata.name}

输出:

Fatal error: Index out of range

Json,当通过 Postman 解析时。

{
"data": {
"id": "b2553208-d89f-4b5d-b08d-5b61dc8d68fe",
"type": "player",
"children": [
{
"id": "legend_8",
"type": "",
"metadata": {
"legend_name": "Pathfinder",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-pathfinder.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 102,
"percentile": 17,
"rank": 8469,
"displayValue": "102",
"displayRank": "8,469"
},
{
"metadata": {
"key": "Damage",
"name": "Damage",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 28845,
"percentile": 8,
"rank": 3455,
"displayValue": "28,845",
"displayRank": "3,455"
},
{
"metadata": {
"key": "Headshots",
"name": "Headshots",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 163,
"percentile": 2.8,
"rank": 1363,
"displayValue": "163",
"displayRank": "1,363"
}
]
},
{
"id": "legend_6",
"type": "",
"metadata": {
"legend_name": "Gibraltar",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-gibraltar.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/gibraltar-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 1,
"percentile": 76,
"rank": 12420,
"displayValue": "1",
"displayRank": "12,420"
},
{
"metadata": {
"key": "Finishers",
"name": "Finishers",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 0,
"rank": 256,
"displayValue": "0",
"displayRank": "256"
}
]
},
{
"id": "legend_2",
"type": "",
"metadata": {
"legend_name": "Bangalore",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-bangalore.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bangalore-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 16,
"percentile": 76,
"displayValue": "16",
"displayRank": ""
},
{
"metadata": {
"key": "Headshots",
"name": "Headshots",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 22,
"percentile": 100,
"rank": 14576,
"displayValue": "22",
"displayRank": "14,576"
},
{
"metadata": {
"key": "Specific2",
"name": "Legend Specific 2",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 3,
"percentile": 100,
"rank": 2392,
"displayValue": "3",
"displayRank": "2,392"
}
]
},
{
"id": "legend_5",
"type": "",
"metadata": {
"legend_name": "Bloodhound",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-bloodhound.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 6,
"percentile": 83,
"displayValue": "6",
"displayRank": ""
},
{
"metadata": {
"key": "Specific2",
"name": "Legend Specific 2",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 4,
"percentile": 100,
"rank": 4007,
"displayValue": "4",
"displayRank": "4,007"
}
]
},
{
"id": "legend_1",
"type": "",
"metadata": {
"legend_name": "Wraith",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-wraith.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/wraith-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 7,
"percentile": 87,
"displayValue": "7",
"displayRank": ""
},
{
"metadata": {
"key": "ArKills",
"name": "AR Kills",
"categoryKey": "weapons",
"categoryName": "Weapons",
"isReversed": false
},
"value": 0,
"rank": 1478,
"displayValue": "0",
"displayRank": "1,478"
}
]
},
{
"id": "legend_4",
"type": "",
"metadata": {
"legend_name": "Mirage",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-mirage.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/mirage-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 8,
"percentile": 84,
"rank": 33915,
"displayValue": "8",
"displayRank": "33,915"
},
{
"metadata": {
"key": "Damage",
"name": "Damage",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 3106,
"percentile": 27,
"rank": 11370,
"displayValue": "3,106",
"displayRank": "11,370"
},
{
"metadata": {
"key": "Headshots",
"name": "Headshots",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 13,
"percentile": 100,
"rank": 5099,
"displayValue": "13",
"displayRank": "5,099"
}
]
},
{
"id": "legend_3",
"type": "",
"metadata": {
"legend_name": "Caustic",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-caustic.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/caustic-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 5,
"percentile": 72,
"rank": 10472,
"displayValue": "5",
"displayRank": "10,472"
},
{
"metadata": {
"key": "Damage",
"name": "Damage",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 2104,
"percentile": 13,
"rank": 2126,
"displayValue": "2,104",
"displayRank": "2,126"
},
{
"metadata": {
"key": "Headshots",
"name": "Headshots",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 10,
"percentile": 4,
"rank": 854,
"displayValue": "10",
"displayRank": "854"
}
]
},
{
"id": "legend_7",
"type": "",
"metadata": {
"legend_name": "Lifeline",
"icon": "https://media.contentapi.ea.com/content/dam/apex-legends/images/2019/01/legends-character-tiles/apex-grid-tile-legends-lifeline.png.adapt.crop16x9.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/lifeline-concept-bg-small.jpg"
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 12,
"percentile": 76,
"displayValue": "12",
"displayRank": ""
}
]
}
],
"metadata": {
"statsCategoryOrder": [
"combat",
"game",
"weapons"
],
"platformId": 5,
"platformUserHandle": "ItsSpress",
"accountId": "b2553208-d89f-4b5d-b08d-5b61dc8d68fe",
"cacheExpireDate": "3/2/2019 9:11:33 AM",
"level": 32
},
"stats": [
{
"metadata": {
"key": "Level",
"name": "Level",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 32,
"percentile": 57,
"displayValue": "32",
"displayRank": ""
},
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 157,
"percentile": 50,
"displayValue": "157",
"displayRank": ""
},
{
"metadata": {
"key": "Damage",
"name": "Damage",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 34055,
"percentile": 36,
"displayValue": "34,055",
"displayRank": ""
},
{
"metadata": {
"key": "Finishers",
"name": "Finishers",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 0,
"displayValue": "0",
"displayRank": ""
},
{
"metadata": {
"key": "ArKills",
"name": "AR Kills",
"categoryKey": "weapons",
"categoryName": "Weapons",
"isReversed": false
},
"value": 0,
"displayValue": "0",
"displayRank": ""
}
]
}
}

如果我做这样的事情......

print(json.data.children.map{$0.stats.map{$0.metadata.name}})

我明白了...

[["Kills", "Damage", "Headshots"], ["Kills", "Finishers"], ["Kills", "Headshots", "Legend Specific 2"], ["Kills", "Legend Specific 2"], ["Kills", "AR Kills"], ["Kills", "Damage", "Headshots"], ["Kills", "Damage", "Headshots"], ["Kills"]]

最佳答案

问题是自己做的。

您正在将组织良好的结构拆分为单独的数组。 不要那样做。从不

保留结构层次结构并从结构中获取值。

好像是children数组代表数据源数组所以删除

<罢工>

<罢工>
var legendImgArr = [""]
//legend name
var legendTitle = [""]
//data set 1
var title1 = [""]
var data1 = [0]
//data set 2
var title2 = [""]
var data2 = [0]
//data set 3
var title3 = ["nil"]
var data3 = [0]

<罢工>

并声明一个数组

var children = [Child]()

并分配[Child] JSON解码后的数组

self.children = json.data.children

同时删除所有为那些已删除数组赋值的行。


为方便起见,创建一个辅助函数以从 stats 中提取数据对于给定的索引:

func nameValue(for stats: [Stat], at index : Int) -> (String, String) {
guard index < stats.count else { return ("", "") }
let stat = stats[index]
return (stat.metadata.name, String(stat.value))
}

然后在cellforRow在索引路径一次

获取 child
let child = children[indexPath.row] 

例如将值分配给标签

cell.legendName.text = child.metadata.legendName
(cell.stat1Title.text, cell.stat1Data.text) = nameValue(for: child.stats, at: 0)
(cell.stat2Title.text, cell.stat2Data.text) = nameValue(for: child.stats, at: 1)
(cell.stat3Title.text, cell.stat3Data.text) = nameValue(for: child.stats, at: 2)

这比无法控制的可怕的独立阵列更高效、更安全。

关于swift - 如何使用 swift 处理 JSON 中丢失的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54956529/

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