Skip to content

Commit 860a732

Browse files
author
Bob Lee
committed
Added sketch file with table of content
1 parent 1272227 commit 860a732

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

course/swift4/codable-json.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,94 @@
11
# Codable and JSON Serialization
22
## Introduction
33
## Problem
4-
4+
Map JSON to Object in one line of code
55

66
## Conclusion
77

8+
```swift
9+
let republicOfKorea = JSON(dictionary: [
10+
"capital": "Seoul",
11+
"name": "Republic of Korea",
12+
"population": 50000000
13+
])
14+
15+
let koreaCaital: String? = republicOfKorea["capital"]
16+
let koreaName: String? = republicOfKorea["name"]
17+
let koreaPopulation: Int? = republicOfKorea["population"]
18+
```
19+
20+
### Import Foundation
21+
```swift
22+
import Foundation
23+
```
24+
25+
### Design Model
26+
27+
```
28+
struct User: Codable {
29+
var name : String
30+
var loginMethods : [LoginMethods]
31+
var numberOfFriends : Int
32+
}
33+
34+
enum LoginMethods: String, Codable {
35+
case facebook, instagram, linkedin, twitter
36+
}
37+
```
38+
39+
### Create Object
40+
```swift
41+
let bob = User(name: "Up", loginMethods: [.facebook, .instagram], numberOfFriends : 4)
42+
```
43+
44+
### Encode (Object to JSON)
45+
46+
```swift
47+
//: Create Encoder
48+
let jsonEncoder = JSONEncoder()
49+
```
50+
51+
#### Customize Encoder
52+
```swift
53+
jsonEncoder.outputFormatting = .prettyPrinted
54+
jsonEncoder.dataEncodingStrategy = .base64Encode
55+
```
56+
57+
#### Execution
58+
```swift
59+
//: Encode
60+
var data: Data?
61+
62+
do {
63+
let jsonData = try jsonEncoder.encode(bob)
64+
data = jsonData
65+
let jsonString = String(data: jsonData, encoding: .utf8)
66+
print("JSON String : " + jsonString!)
67+
}
68+
catch(let error) {
69+
print(error)
70+
}
71+
```
72+
73+
### Decode (JSON to Object)
74+
75+
#### Create Decoder
76+
```swift
77+
let jsonDecoder = JSONDecoder()
78+
```
79+
80+
#### Execution
81+
```swift
82+
do {
83+
let bob = try jsonDecoder.decode(User.self, from: data!)
84+
// let bob = try jsonDecoder.decode(AnotherUser.self, from: data!)
85+
print("Name : \(bob.name)")
86+
print("Number of friends: \(bob.numberOfFriends)")
87+
} catch(let error) {
88+
print(error)
89+
}
90+
```
91+
892
### Source Code
93+
[9004_json_serialization_codable]()
994
### Resources

source-code/9000_swift4/.DS_Store

0 Bytes
Binary file not shown.

source-code/9000_swift4/9004_codable_json.playground/Pages/8001_nested_generics_recursive_enum.xcplaygroundpage/Contents.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/*:
22
## Learn Swift with Bob
33
### What's New in Swift 4
4-
### Codable and JSON Serialization
4+
### 9004_Codable and JSON Serialization
55

66
**Problem:** Map JSON to Object in one line of code
77

88
---
99

1010
*/
11-
//: Import Foundation
12-
import Foundation
1311

1412
//: Your Past
1513
/*
@@ -25,6 +23,10 @@ let koreaPopulation: Int? = republicOfKorea["population"]
2523
*/
2624

2725

26+
//: Import Foundation
27+
import Foundation
28+
29+
2830
//: Design Model
2931
struct User: Codable {
3032
var name : String

update.md

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
- 6002_Filter
5151
- 6003_Map
5252
- 6004_Reduce
53-
- 6005_Flatmap
5453

5554
### Chapter 7: Generic Protocol
5655
- 7001_Intro Protocol Associated Type

0 commit comments

Comments
 (0)