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 a02afe4

Browse files
author
Bob Lee
committed
Finished source code for advanced dictionary
1 parent 4719509 commit a02afe4

File tree

21 files changed

+261
-80
lines changed

21 files changed

+261
-80
lines changed

‎course/.DS_Store‎

0 Bytes
Binary file not shown.

‎course/swift4/generic-subscripts.md‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,53 @@
22

33
## Introduction
44
## Problem
5+
Can subscripts return `T`?
56

7+
### Your past
68
```swift
7-
struct JSON {
8-
fileprivate var 스토리지: [String:Any]
9+
struct WeekDays {
10+
private var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
911

10-
init(dictionary: [String:Any]) {
11-
self.스토리지= dictionary
12+
subscript(index: Int) ->String {
13+
return days[index]
1214
}
1315

14-
subscript<T>(key: String) -> T? {
15-
return 스토리지[key] as? T
16-
}
1716
}
1817
```
1918

20-
19+
### Introducing Generic Subscripts
2120
```swift
22-
let 한국데이터 = JSON(dictionary: [
23-
"수도": "서울",
24-
"나라": "대한민국",
25-
"인구": 50,000,000
26-
])
27-
28-
let 대한민국인구: struct JSON {
29-
fileprivate var 스토리지: [String:Any]
21+
struct JSON {
22+
private var storage: [String: Any]
3023

31-
init(dictionary: [String:Any]) {
32-
self.스토리지 = dictionary
24+
init(dictionary: [String:Any]) {
25+
self.storage = dictionary
3326
}
3427

3528
subscript<T>(key: String) -> T? {
36-
return 스토리지[key] as? T
29+
return storage[key] as? T
3730
}
3831
}
32+
```
3933

40-
let json = JSON(dictionary: [
41-
"수도": "서울",
42-
"나라": "대한민국",
43-
"인구": 50_000_000
34+
### Create JSON
35+
```swift
36+
let republicOfKorea = JSON(dictionary: [
37+
"capital": "Seoul",
38+
"name": "Republic of Korea",
39+
"population": 50000000
4440
])
41+
```
4542

46-
// No need to use as? Int
47-
let 대한민국인구: Int? = 한국데이터["인구"]
48-
let 대한민국수도: String? = 한국데이터["수도"]
43+
### Access Value
44+
```swift
45+
let koreaCaital: String? = republicOfKorea["capital"]
46+
let koreaName: String? = republicOfKorea["name"]
47+
let koreaPopulation: Int? = republicOfKorea["population"]
4948
```
5049

5150

51+
5252
### Source Code
5353
### Resources
5454

‎source-code/.DS_Store‎

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

‎source-code/10000_advanced-swift/10006_associated_type_constraints.playground/Pages/8001_nested_generics_recursive_enum.xcplaygroundpage/Contents.swift‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎source-code/9000_swift4/.DS_Store‎

0 Bytes
Binary file not shown.
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 によって変換されたページ (->オリジナル) /