You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: course/swift4/advanced-dictionary.md
+45-13Lines changed: 45 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,57 @@
1
1
# Advanced Dictionary
2
2
## 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.
5
4
5
+
## Problem
6
+
Functional programming met Swift dictionary
6
7
7
8
### Default Value
9
+
First, let's begin with a warm-up.
10
+
8
11
```swift
9
12
var myInfo = ["name":"Bob Lee", "age":"14"]
10
13
let name = myInfo["name"] // Optional("Bob Lee")
11
14
```
12
15
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
+
13
18
```swift
14
-
let footSize = myInfo["footsize", default: "No Value"] // "No Value"
15
19
let myName = myInfo["name", default: "No Value"] // "Name"
20
+
let footSize = myInfo["footsize", default: "No Value"] // "No Value"
16
21
```
17
22
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.
20
24
21
25
### 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.
22
27
23
28
```swift
24
29
let contacts = ["Julia", "Susan", "John", "Alice", "Alex"]
25
30
let grouped =Dictionary(grouping: contacts, by: { 0ドル.first! })
This is just short and nice. Amazing code. I love it.
108
+
80
109
81
110
## 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.
Copy file name to clipboardExpand all lines: source-code/9000_swift4/9005_advanced_dictionary.playground/Pages/8001_nested_generics_recursive_enum.xcplaygroundpage/Contents.swift
0 commit comments