2014-11-16 172 views
1

這段代碼有什麼問題,我該如何解決這個問題? 請解釋你的答案,讓我可以學習。我對Swift很陌生,對編程也很新穎。Swift中的嵌套字典

var has: Dictionary<String, Dictionary<Dictionary, Int>> = [ 
    "Material" : [ 
     "Factory" : 0, 
     "Raw" : 0, 
     "Energy" : 0, 
    ], 
    "Workers" : [ 
     "Dev" : 0, 
     "Builder" : 0, 
     "Advertiser" : 0, 
     "Consultant" : 0, 
     "Engenier" : 0, 
     "QA" : 0, 
    ], 
    "Assets" : [ 
     "Bonds" : 0, 
    ], 
    "Owe" : [ 
     "10%" = 0, 
     "20%" = 0, 
     "30%" = 0 
    ], 
    "Other" : [ 
     "Insurance" : 0 
    ] 
] 
+1

剛一說明。您可以使用別名'[String:[String:Int]]'來表示字典的類型。更容易閱讀我認爲(並容易發現錯誤(字典)在你的類型字符串 – Fogmeister

回答

8

您聲明型爲Dictionary<String, Dictionary<Dictionary, Int>>,但字面實際上是Dictionary<String, Dictionary<String, Int>>。另外這是非法的語法:

"Owe" : [ 
    "10%" = 0, 
    "20%" = 0, 
    "30%" = 0 
], 

應該是:

"Owe" : [ 
    "10%" : 0, 
    "20%" : 0, 
    "30%" : 0 
], 
+0

太棒了!謝謝,似乎只是語法和我的一個愚蠢的錯誤:) –

+1

或者你可以簡單地省略顯式類型聲明並讓編譯器爲你弄明白。請使用'var a = [']' – sikhapol

+0

@ penne12,請勾選答案旁邊的複選標記以接受答案,將其變爲綠色。 – vacawama