字典,其中value包含字典,值包含数组

时间:2017-04-20 12:12:06

标签: arrays swift dictionary data-structures

我一直在寻找一段时间但没有成功,需要一个答案。

我正在寻找的数据结构是一个具有键和值的字典。值为dictionary,并将数组作为值。

例如:

"San Francisco" -> "Stores", -> "Apple Store", "...", ".." 
                   "Companies" -> "...", ".."
"New York"      -> "Fast Food" -> "Mc", "BK", "KFC"

我怎样才能做到这一点?

我试过

var nest = [String: [String: [String]]]()
nest["New York"]["Fast Food"] = ["MC", "BK"]

这似乎无法正常工作。

但是,我可以这样做

var fastfood = [String: [String]]()
var names = [String]()

fastFood["Fast Food"] = names
nest["New York"] = fastFood

这引起了问题。我每次都需要创建一个对象。我从数据库中获取数据,需要将传入的数据直接附加到嵌套中。

1 个答案:

答案 0 :(得分:1)

如何创建可帮助您管理数据分类的小数据结构。

示例:

struct CityTaxonomy {
    let city: String
    let taxonomy: [String:[String]]
}

CityTaxonomy(city: "San Francisco", taxonomy: ["Stores":["Apple Store", "...", ".."],"Companies":["...", ".."]])