Ios 如何从Dictionary-swift数组中获取数据
如何从该数组获取数据? 这里有一个数组,其中包含一些键值对,一些键包含一个字典数组Ios 如何从Dictionary-swift数组中获取数据,ios,arrays,swift,dictionary,multidimensional-array,Ios,Arrays,Swift,Dictionary,Multidimensional Array,如何从该数组获取数据? 这里有一个数组,其中包含一些键值对,一些键包含一个字典数组 var dataArray = [ ["teamName":"Arsenal", "image":"imageName", "nextMatch":"in 2 days", "matches":[ ["oppo
var dataArray = [
["teamName":"Arsenal",
"image":"imageName",
"nextMatch":"in 2 days",
"matches":[
["oppositeTeam":"teamName",
"matchTimings":"121212",
"matchId":"ID 213432"],
["oppositeTeam":"teamName",
"matchTimings":"121212",
"matchId":"ID 213432"]
],
"fixtures":[
["oppositeTeam":"teamName",
"oppositeTeamScore":"7",
"HomeTeamScore":"4",
"HomeTeamCards":"True",
"oppositeTeamCards":"false",
"fixturesId":"ID 213432"],
]
],["teamName":"Chelsea",
"image":"imageName",
"nextMatch":"in 2 days",
"matches":[["oppositeTeam":"teamName",
"matchTimings":"121212",
"matchId":"ID 213432"],["oppositeTeam":"teamName",
"matchTimings":"121212",
"matchId":"ID 213432"]
],"fixtures":[["oppositeTeam":"teamName",
"oppositeTeamScore":"7",
"HomeTeamScore":"4",
"HomeTeamCards":"True",
"oppositeTeamCards":"false",
"fixturesId":"ID 213432"],["oppositeTeam":"teamName",
"oppositeTeamScore":"7",
"HomeTeamScore":"4",
"HomeTeamCards":"True",
"oppositeTeamCards":"false",
"fixturesId":"ID 213432"]
]
],["teamName":"India",
"image":"imageName",
"nextMatch":"null",
"matches":[],
"fixtures":[]
]]
我尝试过,但无法从此数组中获取数据。使用Codable为数据创建模型。使用JSON解码器解析模型中的数据。然后你可以在任何地方使用你的模型 有关JSON解析,您可以参考本教程:-
您可以像这样从阵列中提取数据:
for attributesObj in dataArray{
let dicFrmArray = attributesObj as! NSDictionary
if ((dicFrmArray["teamName"] as? NSNull) == nil && dicFrmArray["teamName"] != nil){
print(dicFrmArray[teamName"])
}
}
var matches: [Match]
var fixtures: [Fixture]
以下是访问
数据数组中定义的数组/字典的方法:
// To access team object at zero index
if let team = dataArray[0] as? [String: Any] {
print("Team: \(team["teamName"])")
// To access matches array of team object at zero index
if let matches = team["matches"] as? [[String: Any]] {
print( matches)
// To access first match
if let match = matches.first {
print(match)
}
}
// Similar to matches access fixtures
if let fixtures = dataArray[0]["fixtures"] as? [[String: Any]] {
print(fixtures)
// To access first fixture
if let fixture = fixtures.first {
print(fixture)
}
}
}
这是好的,如果你只是原型。如果你打算将其扩展到实际的应用程序中,那么创建单独的模型是最好的方法
您可以拥有一个团队模型,该模型可以包含团队名称、图像、匹配项和装置。对于匹配,可以创建包含匹配信息的模型。同样,也可以为装置创建模型。然后,您的Team类将包含Match和Fixture类的数组,如下所示:
for attributesObj in dataArray{
let dicFrmArray = attributesObj as! NSDictionary
if ((dicFrmArray["teamName"] as? NSNull) == nil && dicFrmArray["teamName"] != nil){
print(dicFrmArray[teamName"])
}
}
var matches: [Match]
var fixtures: [Fixture]
并且您的数据数组将是
var dataArray: [Team]
您需要像这样使用模型
struct Team {
let teamName:String
let image:String
let nextMatch:String
let matches:[Match]?
let fixtures:[Fixture]?
}
struct Match {
let oppositeTeam:String
let matchTimings:String
let matchId:String
}
struct Fixture {
let oppositeTeam:String
let oppositeTeamScore:String
let HomeTeamScore:String
let HomeTeamCards:String
let oppositeTeamCards:String
let fixturesId:String
}
接下来,您需要了解swift中的Codeable
,我在下面附上了一篇文章
您能显示代码吗?您尝试了什么,最终得到了什么。请添加一些您尝试过的代码。数据不是来自API,它只是一个很好的想法-数据在示例和定义中看起来都非常接近JSON。如果您共享,此解决方案可能有效。该模型也将更可靠,更耐破坏。只需要更详细一点……是的,这是我现在使用的JSON的替代品,因为我是swift编程新手。强制展开和NS对象在这一点上对swift来说有点不协调。我认为,如果let或guard语句与将数据作为Swift对象保存在一起,将更好地为OP.@TommieC服务。谢谢你纠正我。。。我会更新这个:)总是很乐意帮忙。如果我的答案解决了您的问题,请接受我的答案。在这种情况下,我们如何迭代dataArray[]@HAKTo迭代使用for循环:用于dataArray中的团队{print(“团队:\(团队[“团队名”)))”)}
很抱歉,我问的是“匹配项”和“装置”中的数据keySincefixture
是一个类型为[String:Any]
的字典,您可以访问类似fixture[“反对派团队”]
的键。最好显示一个用法示例。