- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在从 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
在索引路径一次
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/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!