Add the line pod "Bourne" to your Podfile
Add the line github "nanoxd/Bourne" to your Cartfile
Clone the repo and drag the file Bourne.swift into your Xcode project.
Add the line .Package(url: "https://github.com/nanoxd/Bourne.git", majorVersion: 1) to your Package.swift file.
import Bourne
struct Person {
let firstName: String
let lastName: String
let age: Int?
}
extension Person: Mappable {
static func decode(_ j: JSON) throws -> Person {
return Person(
firstName: try j.from("firstName"),
lastName: try j.from("lastName", or: "Medina"),
age: try? j.from("age")
)
}
}Bourne has two ways of working with Optional values, try? and adding a default value to from.
First, let's look at how to decode a value that is sometimes included in our JSON:
{
"people": [
{
"firstName": "Jose",
"lastName": null
},
{
"firstName": "Maria",
"lastName": "Sanchez",
"age": 20
}
]
}try? j.from("age")