This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = ... | |
do { | |
let user = try JSONDecoder().decode(User.self, from: data) | |
} | |
catch { | |
print(error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Codable { | |
let name: String | |
let surname: String | |
let age: Int | |
} | |
let user = User(name: "John", surname: "Smith", age: 18) | |
do { | |
let userData = try JSONEncoder().encode(user) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let userData = ... | |
do { | |
let json = try? JSONSerialization.jsonObject(with: userData, options: []) | |
} | |
catch { | |
print(error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let user = [ | |
"name": "John", | |
"surname": "Smith", | |
"age": 18 | |
] as [String : Any] | |
do { | |
let userData = try JSONSerialization.data(withJSONObject: user, options: []) | |
} | |
catch { |