2016-06-08 67 views
0

如何快速獲取數組類型字典的鍵。我想輸出作爲 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

給你的情況,你將通過data必須循環和(類型字典的數據陣列的鍵)在每次迭代中獲取數組中第一個對象的鍵。這是因爲它是DictionariesArray,而不是Dictionary。但是,我建議將data變量構造爲Dictionary,除非您的解決方案還有其他更多內容,否則沒有理由不這樣做。

我建議做以下幾點:

data = ["location1":LocationPickerData, "location2":LocationPickerData2, "location3":LocationPickerData3] 

然後,它是微不足道的,讓您的鑰匙:

data.keys 

注:Dictionary不會進行排序。