- 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.
推薦閱讀
- HornetQ Messaging Developer’s Guide
- Servlet/JSP深入詳解
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計算
- Python算法從菜鳥到達人
- 批調(diào)度與網(wǎng)絡(luò)問題的組合算法
- C#程序設(shè)計(項目教學(xué)版)
- Java Web從入門到精通(第2版)
- C++ System Programming Cookbook
- DevOps 精要:業(yè)務(wù)視角
- Java EE 7 Development with WildFly
- 零基礎(chǔ)入門學(xué)習(xí)C語言:帶你學(xué)C帶你飛
- OpenStack Networking Cookbook
- Practical XMPP
- 活文檔:與代碼共同演進
- 區(qū)塊鏈技術(shù)指南