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/generic-subscripts.md
+44-6
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
1
# 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.
2
3
3
4
## Introduction
5
+
4
6
## Problem
5
7
Can subscripts return `T`?
6
8
7
9
### 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
+
8
12
```swift
9
13
structWeekDays {
10
14
privatevar days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
@@ -16,19 +20,29 @@ struct WeekDays {
16
20
}
17
21
```
18
22
23
+
Well, we can make it a little bit more complex and potentially more scalable.
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.
[Smart KeyPaths: Better Key-Value Coding for Swift - Apple](https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md)
74
74
75
75
## 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.
77
77
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.
Copy file name to clipboardExpand all lines: source-code/9000_swift4/9003_generic_subscripts.playground/Pages/9003_generic_subscripts.xcplaygroundpage/Contents.swift
0 commit comments