Swift:访问字典数组中的数组值

时间:2021-05-29 21:28:35

I am currently struggling with obtaining a value from an array inside an array of dictionaries. Basically I want to grab the first "[0]" from an array stored inside an array of dictionaries. This is basically what I have:

我目前正在努力从数组中获取一个数组中的值。基本上,我想从存储在字典数组中的数组中获取第一个“[0]”。这基本上就是我所拥有的:

var array = [[String:Any]]()
var hobbies:[String] = []
var dict = [String:Any]()

viewDidLoad Code:

viewDidLoad代码:

dict["Name"] = "Andreas"
hobbies.append("Football", "Programming")
dict["Hobbies"] = hobbies
array.append(dict)

/// - However, I can only display the name, with the following code:

// -但我只能显示姓名,代码如下:

var name = array[0]["Name"] as! String
  • But I want to be able to display the first value in the array stored with the name, as well. How is this possible?
  • 但是我也希望能够显示数组中存储名称的第一个值。这怎么可能?

And yes; I know there's other options for this approach, but these values are coming from Firebase (child paths) - but I just need to find a way to display the array inside the array of dictionaries.

是的;我知道这种方法还有其他的选项,但是这些值来自于Firebase(子路径)——但是我只需要找到一种方法来显示字典数组中的数组。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


4  

If you know "Hobbies" is a valid key and its dictionary value is an array of String, then you can directly access the first item in that array with:

如果您知道“业余爱好”是一个有效的键,它的字典值是一个字符串数组,那么您可以直接访问该数组中的第一个条目:

let hobby = (array[0]["Hobbies"] as! [String])[0]

but this will crash if "Hobbies" isn't a valid key or if the value isn't [String].

但是,如果“业余爱好”不是有效的键,或者值不是[String],这将导致崩溃。

A safer way to access the array would be:

访问阵列的更安全方法是:

if let hobbies = array[0]["Hobbies"] as? [String] {
    print(hobbies[0])
}

#2


1  

If you use a model class/struct things get easier

如果您使用模型类/结构类,事情会变得更容易

Given this model struct

struct Person {
    let name: String
    var hobbies: [String]
}

And this dictionary

var persons = [String:Person]()

This is how you put a person into the dictionary

let andreas = Person(name: "Andreas", hobbies: ["Football", "Programming"])
persons[andreas.name] = Andreas

And this is how you do retrieve it

let aPerson = persons["Andreas"]

#1


4  

If you know "Hobbies" is a valid key and its dictionary value is an array of String, then you can directly access the first item in that array with:

如果您知道“业余爱好”是一个有效的键,它的字典值是一个字符串数组,那么您可以直接访问该数组中的第一个条目:

let hobby = (array[0]["Hobbies"] as! [String])[0]

but this will crash if "Hobbies" isn't a valid key or if the value isn't [String].

但是,如果“业余爱好”不是有效的键,或者值不是[String],这将导致崩溃。

A safer way to access the array would be:

访问阵列的更安全方法是:

if let hobbies = array[0]["Hobbies"] as? [String] {
    print(hobbies[0])
}

#2


1  

If you use a model class/struct things get easier

如果您使用模型类/结构类,事情会变得更容易

Given this model struct

struct Person {
    let name: String
    var hobbies: [String]
}

And this dictionary

var persons = [String:Person]()

This is how you put a person into the dictionary

let andreas = Person(name: "Andreas", hobbies: ["Football", "Programming"])
persons[andreas.name] = Andreas

And this is how you do retrieve it

let aPerson = persons["Andreas"]