Swift 4 is packed with all kinds of great updates to the Swift Standard Library's Dictionary type thanks to the great work put in by Nate Cook to enhance the Dictionary and Set types in proposal 165 of Swift Evolution. With the beta available for Xcode 9 and the implementation of new APIs available now in the Swift 4 toolchain I thought I would cover a few new dictionary enhancements as well as a few common ones to help you ramp up quicker on what is new with Dictionaries in Swift 4. This tutorial is meant to be a brief overview of how to work with some new and existing APIs and is not meant to be an in-depth analysis on proposal 165 for Swift Evolution.
NOTE: This tutorial assumes you have at least Swift 4.0 installed in your Xcode toolchain or that you are using a version of Xcode that supports Swift 4, like Xcode 9 beta. To get started with this tutorial you can know a lot about Swift or a little about Swift, these examples are aimed at everyone.
1) Dictionary Creation
The first two examples of dictionary creation may look familiar as these are Swift 3, Xcode 8 supported methods for dictionary creation. The first creates a new mutable dictionary type with a string definition as the key and value. After the dictionary is created on line 6 the subscript syntax is used to explicitly assign a value to a key.
The second example creates a dictionary literal where the integer keys and values are explicitly assigned during the dictionary creation. These examples often work well when manipulating small bits of data in your program.
The third example is new to Swift 4 and came out of proposal 165. In this example an array of tuples is created to simulate key/value pairs in a dictionary that contain conflicting keys. If you notice, the first two tuples contain the "one" key which typically is not supported when creating a dictionary. This new Dictionary initializer now enhances dictionary uniqueness by using a closure to filter the first duplicate keys in a dictionary. Notice that in the output that the "one" key contains the number one instead of two.
["three": 3, "four": 4, "five": 5, "one": 1, "two": 2]
The fourth example is new to Swift 4 and came out of proposal 165. In this example an array of key strings is created as well as an array of integers that will ultimately be used to create a new dictionary of even key / value pairs. On line 34 you will notice that the numeric sequence is filtered to extract just the even values and in line 35 a new dictionary initializer is then used to map a unique set of keys and values together to form the evenDict dictionary with an output of:
["six": 6, "four": 4, "eight": 8, "ten": 10, "two": 2]
2) Dictionary Modification
The first three examples of dictionary modification may look familiar as these are Swift 3, Xcode 8 supported methods for dictionary mutation. The first is just straight forward mutation via assigning a value to a subscript.
In the second example, an update is performed by using the API updateValue(_:forKey:) to update a value at a specific key. Notice that the current value is assigned to an optional as this value could be nil if the key was not present in the dictionary.
In the third example we are removing a value in a dictionary much like we updated one, by using the API removeValue(forKey:) instead to do so. Notice that in the removal example the value that was removed is also assigned to an optional value to validate that, in fact, the value removed and nil was not returned.
The forth example is new to Swift 4 and came out of proposal 165 as a new way to allow developers to merge a sequence of name value pairs into a dictionary. You will notice that in the example I have the existing dictionary and a sequence of tuples that uses a closure to sort the current dictionary values as duplicates and merge all other new data from the sequence. The following example outputs:
["three": 6, "four": 4, "five": 5, "one": 2, "two": 2]
This is a very cool addition to the dictionary type in Swift 4, but I must warn you that the documentation is pretty confusing in some cases and to make sure you are merging a sequence and not a new dictionary.
3) Dictionary Grouping
The two grouping examples are new to Swift 4 and came out of proposal 165 as a new way to allow developers to now group a sequence of values into a dictionary that explains their grouping. The first example take the people array and creates a dictionary out of the sequence containing the first letter of the names as the key and an array as the value. The output of the first example is:
["R": ["Rich"], "K": ["Karin", "Ken"], "M": ["Matt", "Mary", "Mike"], "P": ["Phil"], "E": ["Edward"]]
The second example works much like the first, but instead of using the letter of the name as the key, the length is instead used. This functionality provides rich word grouping functionality when a procedure like language processing is performed. The output of the second example is:
[5: ["Karin"], 6: ["Edward"], 4: ["Matt", "Rich", "Mary", "Mike", "Phil"], 3: ["Ken"]]
Now you know more about how to create and manipulate dictionaries in Swift 4! Please feel free to download the playground example from my Github and mess around with it. One thing to note again is that if your version of Xcode does not support Swift 4 yet, you will need to download the Swift 4 toolchain to run this playground in Xcode.
Thank you very much for reading!
|818||Develop||[ios] Locale Identifiers||hooni||2018.11.29||232|
|»||Develop||[ios] Swift 4 Dictionary 사용하기||hooni||2018.11.29||259|
|816||Develop||[ios] Swift 4 Singleton inheritance||hooni||2018.10.31||307|
|815||Develop||[ios] APNS, Remote Push 사용자가 수신을 동의했는지 확인하기||hooni||2018.10.19||274|
|814||Develop||[ios] APNS, Remote Push 수신 시점에서 앱의 3가지 실행 상태||hooni||2018.10.19||154|
|813||Develop||[ios] Swift 4 String, Date, DateFormatter 예제||hooni||2018.10.18||145|
|812||Develop||[ios] Requesting Location Permissions in iOS||hooni||2018.08.18||224|
|811||Develop||[PHP] Mac OS에서 PHP 7 설치하기||hooni||2018.05.11||497|
|810||Develop||[php] Connect to Firebase Console in Laravel||hooni||2018.05.09||432|
|809||Develop||[api] 인스타그램 API Access_Token 발급 방법 (Instagram API) 2||hooni||2018.04.05||2961|
|808||Develop||[api] 인스타그램에서 최신 이미지 가져오기 (Using Instagram API) 17||hooni||2018.04.05||5179|
|807||Develop||[js] Click button copy to clipboard||hooni||2018.04.05||291|