官术网_书友最值得收藏!

Convert tuples to Dictionary

With Swift 4, you can now create a new unique Dictionary from an array of tuples consisting of duplicate keys. Let's take an example of an array of tuples with duplicate keys:

let tupleWithDuplicateKeys = [("one", 1), ("one", 2), ("two", 2), ("three", 3), ("four", 4), ("five", 5)]

 Also, you want to convert this array into Dictionary, so you can do this:

let dictionaryWithNonDuplicateKeys = Dictionary(tupleWithDuplicateKeys, uniquingKeysWith: { (first, _) in first })

Now if you try to print dictionaryWithNonDuplicateKeys;

print(dictionaryWithNonDuplicateKeys), the output will be as illustrated:

["three": 3, "four": 4, "five": 5, "one": 1, "two": 2],

This is along with all the duplicate keys removed in the resulting Dictionary.

主站蜘蛛池模板: 关岭| 堆龙德庆县| 泉州市| 洛宁县| 宝丰县| 新民市| 棋牌| 鸡西市| 涞源县| 海宁市| 芦溪县| 成安县| 赤壁市| 青岛市| 黔西县| 湾仔区| 武宁县| 伊宁县| 龙口市| 台安县| 高淳县| 玛沁县| 内江市| 枣强县| 泉州市| 杭州市| 邹城市| 梨树县| 三明市| 江西省| 龙江县| 昌图县| 略阳县| 凤山县| 庆云县| 买车| 定安县| 井冈山市| 桦南县| 北川| 青铜峡市|