Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c94f5cd

Browse files
author
Bob Lee
committed
Finished doc for Advanced Swift
1 parent d049207 commit c94f5cd

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

‎course/swift4/advanced-dictionary.md‎

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
# Advanced Dictionary
22
## Introduction
3-
## Problem
4-
3+
Welcome to Lesson 5, Advanced Dictionary of What's New in Swift 4. Before I say anything about this lesson. I have you warn you what would happen by the time the you will have completed this course. First, you would feel a sense of superiority, even feel over-confidence because most developers do not code the way you will. Let's find out what I mean by the previous statement.
54

5+
## Problem
6+
Functional programming met Swift dictionary
67

78
### Default Value
9+
First, let's begin with a warm-up.
10+
811
```swift
912
var myInfo = ["name": "Bob Lee", "age": "14"]
1013
let name = myInfo["name"] // Optional("Bob Lee")
1114
```
1215

16+
When you attempt to access a value, the dictionary return an optional type. In Swift 4, however, you may prevent that from happening.
17+
1318
```swift
14-
let footSize = myInfo["footsize", default: "No Value"] // "No Value"
1519
let myName = myInfo["name", default: "No Value"] // "Name"
20+
let footSize = myInfo["footsize", default: "No Value"] // "No Value"
1621
```
1722

18-
> I'd use it everywhere.
19-
23+
> I'd use it everywhere since I do not like optionals floating around in my codebase. Optional types represent death for other lines of code. To prevent death, you have to add more lines of code.
2024
2125
### Grouping a sequence into a dictionary
26+
The `Dictionary` struct provides a couple custom initializers that allow you to create a dictionary using an array.
2227

2328
```swift
2429
let contacts = ["Julia", "Susan", "John", "Alice", "Alex"]
2530
let grouped = Dictionary(grouping: contacts, by: { 0ドル.first! })
2631

2732
grouped // ["J": ["Julia", "John"], "S": ["Susan"], "A": ["Alice", "Alex"]]
33+
```
34+
35+
I don't exactly know the custom init method looks like. As the parameter indicates, it requires the value of each element in the array.
2836

2937

38+
### Unique Key
39+
You may also create an array filled with tuple to a dictionary with unique keys.
40+
41+
```swift
3042
let duplicates = [("a", 1), ("b", 7), ("a", 3), ("b", 4)]
31-
let myLetter = Dictionary(duplicates, uniquingKeysWith: { (first, _) in first })
43+
let myLetter = Dictionary(duplicates, uniquingKeysWith: { (letter, number) in letter })
3244
uniquingKeysWith // ["b": 7, "a": 1]
3345
```
3446

47+
What a powerful way to manipulate an array of tuples.
48+
3549
### Zip (Dictionary Like)
50+
Some may not be familiar with `Zip`. Let's go over before moving on to the next step.
51+
3652
```swift
3753
let words = ["one", "two", "three", "four"]
38-
let numbers = 1...4
54+
let numbers = 1...words.count
3955

4056
for (word, number) in zip(words, numbers) {
4157
print("\(word): \(number)")
@@ -46,39 +62,55 @@ for (word, number) in zip(words, numbers) {
4662
// Prints "four: 4"
4763
```
4864

49-
Zip to Dictionary
65+
For `zip`, the parameters must conform to the `sequence` protoocol. You will learn more about it in the next chapter.
66+
67+
### Zip to Dictionary
68+
Let us apply `Zip` to create a beautiful dictionary.
69+
5070
```swift
5171
let friends = ["Hoy", "Dan", "Huy"]
5272
let friendsWithValue = Dictionary(uniqueKeysWithValues: zip(1..., friends))
5373
```
54-
74+
> You don't need to memorize. All you need to know is, "things" exist.
5575
5676
### Functional Programming with Set
77+
In the past, when you apply built-in functions such as `filter`, it returns `Array<T>`. In Swift, it returns `Set<T>`
78+
5779
```swift
5880
let set: Set = [1,2,3,4,5]
5981
let filteredSet = set.filter { 0ドル % 2 == 0 } // Returns Set
60-
type(of: filteredSet)
82+
type(of: filteredSet)// Set
6183
```
6284
### Functional Programming with Dictionary
85+
Now, this is the real deal. You will never the code the same way if you understand this. But, again, this should be a review of the previous chapters.
6386

64-
```
87+
```swift
6588
let dictionary = ["name": "Bob", "age": "15", "hairColor": "Black"]
6689

6790
let filtered = dictionary.filter {
6891
0ドル.key == "name"
6992
}
7093

71-
print(filtered)
94+
print(filtered) // ["name": "Bob"]
95+
```
96+
97+
This is just purely beautiful and nice.
7298

99+
```swift
73100
let mapped = dictionary.mapValues { value in
74101
value.uppercased()
75102
}
76103

77-
print(mapped)
104+
print(mapped)// ["name": "BOB", "age": "15", "hairColor": "BLACK"]
78105
```
79106

107+
This is just short and nice. Amazing code. I love it.
108+
80109

81110
## Conclusion
111+
That's it. That's just it. As I promised, you will never code the way you've been doing in the past. In fact, Swift 4 has changed my perspectives towards how to manipulate key-value in Dictionary. But, still, some may feel overwhelmed by the amount of information that I've presented to you thus far. Don't you worry. I do not want you to memorize. In fact, memorization isn't needed anymore in this world where the internet and your left hand, Google is available 24/7. As I mentioned, all you need to know is, "I've been it before and those things exist". That's it. All you have to do is search and learn more!
112+
113+
Do not try to memorize any, simply understand the fundamentals and look at API guidelines and references for your usage. At least it works out for me.
82114

83115
### Source Code
84116
[9005_advanced_dictionary.playground]()

‎source-code/9000_swift4/.DS_Store‎

0 Bytes
Binary file not shown.

‎source-code/9000_swift4/9005_advanced_dictionary.playground/Pages/8001_nested_generics_recursive_enum.xcplaygroundpage/Contents.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ let grouped = Dictionary(grouping: contacts, by: { 0ドル.first! })
2727
print(grouped)
2828

2929

30-
let duplicates = [("a", 1), ("b", 7), ("a", 3), ("b", 4)]
31-
let myLetter = Dictionary(duplicates, uniquingKeysWith: { (first, _) in first })
30+
let duplicates = [("a", 1), ("b", 7), ("a", 3), ("b", 10)]
31+
let myLetter = Dictionary(duplicates, uniquingKeysWith: { (letter, number) in letter })
3232
print(myLetter)
3333

3434
//: ### Zip: (Dictionary Like)
3535
let words = ["one", "two", "three", "four"]
36-
let numbers = 1...4
36+
let numbers = 1...words.count
3737

3838
for (word, number) in zip(words, numbers) {
3939
print("\(word): \(number)")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='6.0' target-platform='ios' display-mode='raw'/>
2+
<playground version='6.0' target-platform='ios' display-mode='rendered'/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /