Skip to content

Instantly share code, notes, and snippets.

let user = [
"name": "John",
"surname": "Smith",
"age": 18
] as [String : Any]
do {
let userData = try JSONSerialization.data(withJSONObject: user, options: [])
}
catch {
let userData = ...
do {
let json = try? JSONSerialization.jsonObject(with: userData, options: [])
}
catch {
print(error)
}
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)
}
let data = ...
do {
let user = try JSONDecoder().decode(User.self, from: data)
}
catch {
print(error)
}