-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
111 lines (101 loc) · 2.92 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//: [Previous Topic](@previous)
/*:
# Playground for Presenting
# Customize `CodingKey`
*/
import Foundation
//: ### Decoding From JSON string to Message Object which contain a from Person object
//: ### and customize `CodingKeys`
//:1. Create A simple json `Data` from `JSON` String
/*:
A Message JSON Object
```
{
"message": "Hi, How are you?",
"create": 1523895892938,
"from": {
"name" : "Ali",
"age":25,
"email":"[email protected]",
"address":"Iran, Tehran"
}
}
```
*/
let jsonData = """
{
"message": "Hi, How are you?",
"create": 1523895892938,
"from": {
"name" : "Ali",
"age":25,
"email":"[email protected]",
"address":"Iran, Tehran"
}
}
""".data(using: .utf8)!
//:2. Define a struct and called Message which adapt to `Codable` Protocol
struct Message : Codable {
let message : String
let create : TimeInterval
let sender : Person
/*:
3. Declared CodingKeys\
which is `enum` of `String` and adapt to `CodingKey` protocol\
[CodingKey Apple Document](https://developer.apple.com/documentation/swift/codingkey)
*/
private enum CodingKeys : String, CodingKey {
//: * message represent of *message* property
case message
//: * create represent of *create* property
case create
/*:
* sender represent of *sender* property but\
we need to set value of *sender* case to "from"\
**Note:**"from" is json key that present in message json string
*/
case sender = "from"
}
//:4. Define a nested struct and named Person which adapted to `Codable` Protocol
struct Person : Codable {
let name : String
let age : Int
let email : String
let address : String
/*:
**Note:** CodingKeys of `Person` struct will be generated by compiler\
so we dont need to customize *CodingKeys*
private enum CodingKeys : String, CodingKey {
case name
case age
case email
case address
}
*/
}
}
//:5. defind jsonDecoder as JSONDecoder
let jsonDecoder = JSONDecoder()
do {
/*:
6. we use `-decode` method to decode `jsonData` into `Message` object with `try` option
- first parameter is type of Object which want convert into such as `Int`, `String`,
or Declared `struct` or `class`. The example is `Int.self`
- second parameter is `jsonData' which we want convert from it into our definded type
*/
let message = try jsonDecoder.decode(Message.self,from:jsonData)
//:7. print Decoded `Message` Object
print("""
message = \(message.message)
create = \(message.create)
sender = \(message.sender)
\n
""")
} catch {
/*:
8. Handling Error
* error occured while decoding json to message swift struct
*/
print("Error on decoding \(error)")
}
//: [Next Topic](@next)