- Reactive Programming with Swift 4
- Navdeep Singh
- 114字
- 2021-06-24 18:57:58
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.
推薦閱讀
- 樂高機器人設(shè)計技巧:EV3結(jié)構(gòu)設(shè)計與編程指導(dǎo)
- Groovy for Domain:specific Languages(Second Edition)
- Python程序設(shè)計案例教程
- 劍指Java:核心原理與應(yīng)用實踐
- Unity 2D Game Development Cookbook
- Unity UI Cookbook
- Learning AngularJS for .NET Developers
- Mockito Essentials
- Practical Microservices
- 計算機系統(tǒng)解密:從理解計算機到編寫高效代碼
- 走近SDN/NFV
- Mastering React Test:Driven Development
- R High Performance Programming
- 程序員的算法趣題2
- Python從入門到項目實踐(超值版)