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
{ | |
"name": "Luke Freaking Skywalker", | |
"age": 22, | |
"address": "Moisture Farm, Tatooine" | |
} |
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 encoder = JSONEncoder() | |
do { | |
let data = try encoder.encode(BattleShip) | |
print(String(data: data, encoding: .utf8)!) | |
} catch { | |
print("error in converting the model") | |
} |
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
{ | |
"name": "Death Star", | |
"model": "DS-1 Orbital Battle Station", | |
"manufacturer": "Imperial Department of Military Research, Sienar Fleet Systems", | |
"hyperdrive_rating": 4.0, | |
"starship_class": "Deep Space Mobile Battlestation", | |
"created": "2014-12-10T16:36:50.509000Z", | |
"url": "http://swapi.co/api/starships/9/" | |
} |
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 Person: Codable { | |
var name: String | |
var age: Int | |
var address: String | |
} |
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 jsonData = Person JSON | |
let decoder = JSONDecoder() | |
do { | |
let model = try decoder.decode(Person.self, from: jsonData) | |
print(model) | |
} catch { | |
print("Error parsing JSON") | |
} |
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 BattleShip: Codable { | |
var name: String | |
var model: String | |
var manufacturer: String | |
var hyperdriveRating: Double | |
var starshipClass: String | |
var created: String | |
var url: String | |
enum CodingKeys: String, CodingKey { |
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
{ | |
"name": "Death Star", | |
"created": "2014-12-10T16:36:50.509000Z", | |
"url": "http://swapi.co/api/starships/9/", | |
"films": [ | |
{ | |
"name": "Star Wars: Episode IV – A New Hope", | |
"director": "George Lucas" | |
}, | |
{ |
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 BattleShip: Codable { | |
var name: String | |
var created: String | |
var url: String | |
struct Film: Codable { | |
var name: String | |
var director: String | |
} | |
var films: [Film] | |
} |
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 decoder = JSONDecoder() | |
do { | |
let films = try decoder.decode([Film].self, from: jsonData) | |
} catch { | |
print("parse 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 BattleShip: Codable { | |
//... | |
var created: Date | |
//... | |
} | |
//... | |
// Let's decode | |
let decoder = JSONDecoder() | |
do { |
OlderNewer