- 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.
推薦閱讀
- Data Visualization with D3 4.x Cookbook(Second Edition)
- C++ Builder 6.0下OpenGL編程技術
- Java入門很輕松(微課超值版)
- RabbitMQ Cookbook
- Odoo 10 Implementation Cookbook
- Geospatial Development By Example with Python
- Instant Debian:Build a Web Server
- C指針原理揭秘:基于底層實現機制
- 測試架構師修煉之道:從測試工程師到測試架構師
- UML軟件建模
- iOS Development with Xamarin Cookbook
- Building a Media Center with Raspberry Pi
- Microsoft Windows Identity Foundation Cookbook
- 現代JavaScript編程:經典范例與實踐技巧
- 軟件測試實驗實訓指南