-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
96 lines (82 loc) · 2.41 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
//: [Table Of Contents](@previous)
//: # Playground for presenting Simple Codable
import UIKit
import Foundation
//: ### Decoding From JSON string to Person Object in Swift
//:1. Create A simple json `Data` from `JSON` String
/*:
A Simple Person JSON Object
```
{
"name" : "Ali",
"age":25,
"email":"[email protected]",
"address":"Iran, Tehran"
}
```
*/
let jsonData = """
{
"name" : "Ali",
"age":25,
"email":"[email protected]",
"address":"Iran, Tehran"
}
""".data(using: .utf8)!
//:2. Define a struct and called Person which adapt to `Codable` Protocol
struct Person : Codable {
let name : String
let age : Int
let email : String
let address : String
}
//:3. defind jsonDecoder as JSONDecoder
let jsonDecoder = JSONDecoder()
do {
/*:
4. we use `-decode` method to decode `jsonData` into person 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 person = try jsonDecoder.decode(Person.self,from:jsonData)
//:5. print Decoded Person Object
print("""
name = \(person.name)
age = \(person.age)
email = \(person.email)
address = \(person.address)
\n
""")
} catch {
/*:
6. Handling Error
* error occured while decoding json to person swift struct
*/
print("Error on decoding \(error)")
}
//: ### Encoding a Person to json String
//:1. declare a new person
let person = Person(name:"Hadi",age:35,email:"[email protected]",address:"Iran, Tabriz")
//:2. declare a jsonEncoder which is `JSONEncoder`
let jsonEncoder = JSONEncoder()
//:3. we set outputFormatting to `.prettyPrinted`
jsonEncoder.outputFormatting = .prettyPrinted
do {
/*:
4. we use `-encode` method to ecode `person` into jsonData
5. all we must do, is send our person object as parameter to `encode` method
*/
let encodedData = try jsonEncoder.encode(person)
//: 6. Convert `encodedData`,which represent of json data of `person` that we convert it, to jsonString
let jsonString = String(data:encodedData,encoding:.utf8)!
//:7. print JSON String
print(jsonString)
}catch {
/*:
8. Handling Error
* error occured while decoding json to person swift struct
*/
print("Error on encoding \(error)")
}
//: [Next Topic](@next)