如何在swift中获取类型字典数组的键。我希望输出为 location1,location2,location3(类型字典的数据数组的键)
//我的代码在下面给出
class ViewController: UIViewController
{
var data = [NSDictionary]()
var LocationPickerData:[String] = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var LocationPickerData2 = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
var LocationPickerData3 = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
override func viewDidLoad() {
super.viewDidLoad()
data = [["lacation1":LocationPickerData],["location2":LocationPickerData2],["location2":LocationPickerData3]]
print(data[keys])
}
}
答案 0 :(得分:0)
在您的情况下,您将不得不遍历data
并在每次迭代中获取数组中第一个对象的键。那是因为它是Array
Dictionaries
,而不是Dictionary
。但是,我建议将data
变量构造为Dictionary
,除非您的解决方案还有更多内容,否则没有理由不这样做。
我建议你做以下事情:
data = ["location1":LocationPickerData, "location2":LocationPickerData2, "location3":LocationPickerData3]
然后,获得你的钥匙是微不足道的:
data.keys
注意:Dictionary
不会被排序。