Skip to content

Commit 1272227

Browse files
author
Bob Lee
committed
Finished doc for keypaths
1 parent 3f0c24b commit 1272227

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

course/swift4/generic-subscripts.md

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Generic Subscripts
2+
Welcome to one of the least favored lessons of all time. Hopefully, this lesson is nice and short since I don't like using subscripts at all. Let's get started.
23

34
## Introduction
5+
46
## Problem
57
Can subscripts return `T`?
68

79
### Your past
10+
You remember in Chapter 1, this is probably how I introduced you to subscripts in Swift, whose role is to provide a shortcut.
11+
812
```swift
913
struct WeekDays {
1014
private var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
@@ -16,19 +20,29 @@ struct WeekDays {
1620
}
1721
```
1822

23+
Well, we can make it a little bit more complex and potentially more scalable.
24+
25+
### JSON
26+
A `json` file looks like below.
27+
28+
```json
29+
{
30+
"name": "Bob",
31+
"age": 21,
32+
"favoriteFood": ["Pizza", "Carbonara", "Hamburger"]
33+
}
34+
```
35+
1936
### Introducing Generic Subscripts
37+
Let us create how to "design" a `json` like structure in Swift by using `Dictionary`.
38+
2039
```swift
2140
struct JSON {
2241
private var storage: [String: Any]
2342

2443
init(dictionary: [String: Any]) {
2544
self.storage = dictionary
2645
}
27-
28-
subscript<T>(key: String) -> T? {
29-
return storage[key] as? T
30-
}
31-
}
3246
```
3347

3448
### Create JSON
@@ -40,16 +54,40 @@ let republicOfKorea = JSON(dictionary: [
4054
])
4155
```
4256

57+
### Introducing Generic Subscripts
58+
```swift
59+
struct JSON {
60+
private var storage: [String: Any]
61+
62+
init(dictionary: [String: Any]) {
63+
self.storage = dictionary
64+
}
65+
66+
subscript<T>(key: String) -> T? {
67+
return storage[key] as? T
68+
}
69+
}
70+
```
71+
72+
4373
### Access Value
74+
Let us use the generic subscript method above.
4475
```swift
45-
let koreaCaital: String? = republicOfKorea["capital"]
76+
let koreaCapital: String? = republicOfKorea["capital"]
4677
let koreaName: String? = republicOfKorea["name"]
4778
let koreaPopulation: Int? = republicOfKorea["population"]
4879
```
4980

81+
Make sure you define `T?` from `any` explicitly as shown as `koreaCapital: String?`.
5082

83+
### My Opinion
84+
I don't recommend using Subscripts even if it provides generics because there isn't much context available and it takes much effort to search through.
5185

5286
### Source Code
87+
[9003_generic_subscripts]()
88+
5389
### Resources
90+
[Generic Subscripts - Apple](https://github.com/apple/swift-evolution/blob/master/proposals/0148-generic-subscripts.md)
5491

5592
## Conclusion
93+
Just to recap, in the very first chapter, you've learned that Swift provides a shortcut for accessing methods and properties. However, I warned you because that shortcut comes along with a big tradeoff. First, it is hard to found and second, it does not provide context unless the name of an object does. Also, even if you use generic subscripts, I don't like the fact that I have to explicitly define the placeholder type of `T`. It definitely can cause a huge issue for your teammates if they have not read or understand the object he/she is working with. So, I recommend not to use them. But, of course, if it is something quick and easy. feel free to.

course/swift4/keypaths.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ rob[keyPath: nameKeyPath] // "Rob"
7373
[Smart KeyPaths: Better Key-Value Coding for Swift - Apple](https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md)
7474

7575
## Conclusion
76-
Congratulations. Remember, even if you are chaining through, you may make a single mistake that leads to catastrophic result. Although it is a brand new feature, if you need to access Property or Method through many chainings, I recommend you to utilize the keypath feature provided in Swift 4.
76+
Congratulations. Remember, even if you are chaining through, you may make a single auto-completion mistake that leads to catastrophic result. Although it is a brand new feature, if you need to access Property or Method through many chainings, I recommend you to utilize the keypath API provided in Swift 4. If you want to learn much deeper, feel free to take a look at the Apple's proposal documentation in the lecture notes.
7777

78-
In the following lesson, you will learn how to make subscripts generic.
78+
In the following lesson, you will learn how to make subscripts generic, which isn't that important but at least you can brag from what you've learned.

source-code/9000_swift4/.DS_Store

0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*:
22
## Learn Swift with Bob
33
### What's New in Swift 4
4-
### Generic Subscripts
4+
### 9003_Generic Subscripts
55

66
**Problem:** Can subscripts return `T`?
77

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)