swift - How to use ExSwift Array.toDictionary? -


exswift has array extension:

    /**     converts array dictionary keys , values supplied via transform function.      :param: transform     :returns: dictionary */ func todictionary <k, v> (transform: (element) -> (key: k, value: v)?) -> [k: v] {     var result: [k: v] = [:]     item in self {         if let entry = transform(item) {             result[entry.key] = entry.value         }     }      return result } 

the exswift wiki shows example other todictionary() method signature. i'm not yet familiar way how these method signatures work. wondering whether can show me example of how use above method call?

you need provide closure takes parameter element of array , returns key - value pair.

say have array of keys, , want create dictionary default value each key (let's 0):

let keys = ["a", "b", "c"] let dictionary = keys.todictionary { element in     return (element, 0) } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -