2015-08-15 85 views
4

其他語言如Python讓你使用字典理解從數組中做出字典,但我還沒有弄清楚在Swift中如何做到這一點。我以爲我可以使用這樣的,但它不會編譯:Swift詞典理解

let x = ["a","b","c"] 
let y = x.map({ ($0:"x") }) 
// expected y to be ["a":"x", "b":"x", "c":"x"] 

什麼是產生在迅速數組的字典正確的方法是什麼?

回答

4

map方法簡單地將數組中的每個元素轉換爲新元素。但結果仍然是一個數組。要將數組轉換爲字典,可以使用reduce方法。

let x = ["a","b","c"] 
let y = x.reduce([String: String]()) { (var dict, arrayElem) in 
    dict[arrayElem] = "this is the value for \(arrayElem)" 
    return dict 
} 

這將生成字典

["a": "this is the value for a", 
"b": "this is the value for b", 
"c": "this is the value for c"] 

一些說明:的reduce第一個參數是初始值,其在這種情況下是空的字典[String: String]()reduce的第二個參數是將數組的每個元素組合爲當前值的回調函數。在這種情況下,當前值是字典,我們爲每個數組元素定義一個新的鍵和值。修改過的字典也需要在回調中返回。


更新:由於reduce方法可以在內存對於大型陣列(見註釋),您也可以定義類似下面的代碼段的自定義功能的理解沉重。

func dictionaryComprehension<T,K,V>(array: [T], map: (T) -> (key: K, value: V)?) -> [K: V] { 
    var dict = [K: V]() 
    for element in array { 
     if let (key, value) = map(element) { 
      dict[key] = value 
     } 
    } 
    return dict 
} 

調用該函數看起來像這樣。

let x = ["a","b","c"] 
let y = dictionaryComprehension(x) { (element) -> (key: String, value: String)? in 
    return (key: element, value: "this is the value for \(element)") 
} 

更新2:取而代之的是自定義函數,你也可以定義上Array的擴展,它會使代碼更容易重用。

extension Array { 
    func toDict<K,V>(map: (T) -> (key: K, value: V)?) -> [K: V] { 
     var dict = [K: V]() 
     for element in self { 
      if let (key, value) = map(element) { 
       dict[key] = value 
      } 
     } 
     return dict 
    } 
} 

調用上述將看起來像這樣。

let x = ["a","b","c"] 
let y = x.toDict { (element) -> (key: String, value: String)? in 
    return (key: element, value: "this is the value for \(element)") 
} 
+0

請注意,這會在每個縮小步驟中創建一個新字典。如果應用於* large *數組,這可能是性能問題。 –

+0

@MartinR是因爲字典是通過值傳遞給回調的,因爲它是一個內部結構的事實? – hennes

+0

是的,確切地說。請參閱此評論:http://stackoverflow.com/questions/24116271/whats-the-cleanest-way-of-applying-map-to-a-dictionary-in-swift#comment47086028_28502842。 –