lightsaber.color = .blue
Table of Contents
Ignite
Today’s lecture was designed around learning how to use JSON apis and we made a sample app that did as such. I’ve done this kind of thing before, but not extensively…
Vwhoosh
But, in my limited experience, somehow I had no idea that there was a free database out there full of Star Wars data! That’s right, my school is so cool we made an app in class to search a Star Wars database for characters! The site the database is hosted on is called SWAPI. (Naturally, as I’m writing this, the site went down. It already happened, so I can’t even take credit for it with my massive viewer numbers!)
Clash!
I made an incomplete model for a character, if anyone is interested at all.
struct Person: Decodable {
let name: String
let gender: Gender
let birthYear: String
let height: Int
let mass: Int
let hairColor: String
let skinColor: String
let eyeColor: String
enum Gender: String {
case male
case female
case na
static func genderForString(_ string: String) -> Gender {
let lc = string.lowercased()
switch lc {
case "male":
return .male
case "female":
return .female
default:
return .na
}
}
}
}
extension Person {
enum CodingKeys: String, CodingKey {
case name
case gender
case birthYear = "birth_year"
case height
case mass
case hairColor = "hair_color"
case skinColor = "skin_color"
case eyeColor = "eye_color"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
let genderStr = try container.decode(String.self, forKey: .gender)
gender = Gender.genderForString(genderStr)
birthYear = try container.decode(String.self, forKey: .birthYear)
let heightStr = try container.decode(String.self, forKey: .height)
height = Int(heightStr) ?? 0
let massStr = try container.decode(String.self, forKey: .mass)
mass = Int(massStr) ?? 0
hairColor = try container.decode(String.self, forKey: .hairColor)
skinColor = try container.decode(String.self, forKey: .skinColor)
eyeColor = try container.decode(String.self, forKey: .eyeColor)
}
}
struct PersonSearch: Decodable {
let results: [Person]
}
Hummmmmmm
If that’s not enough for you, I suppose you can have the rest of the project. It’s not much, but it’s fun!
Ignite
Today’s lecture was designed around learning how to use JSON apis and we made a sample app that did as such. I’ve done this kind of thing before, but not extensively…
Vwhoosh
But, in my limited experience, somehow I had no idea that there was a free database out there full of Star Wars data! That’s right, my school is so cool we made an app in class to search a Star Wars database for characters! The site the database is hosted on is called SWAPI. (Naturally, as I’m writing this, the site went down. It already happened, so I can’t even take credit for it with my massive viewer numbers!)
Clash!
I made an incomplete model for a character, if anyone is interested at all.
struct Person: Decodable {
let name: String
let gender: Gender
let birthYear: String
let height: Int
let mass: Int
let hairColor: String
let skinColor: String
let eyeColor: String
enum Gender: String {
case male
case female
case na
static func genderForString(_ string: String) -> Gender {
let lc = string.lowercased()
switch lc {
case "male":
return .male
case "female":
return .female
default:
return .na
}
}
}
}
extension Person {
enum CodingKeys: String, CodingKey {
case name
case gender
case birthYear = "birth_year"
case height
case mass
case hairColor = "hair_color"
case skinColor = "skin_color"
case eyeColor = "eye_color"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
let genderStr = try container.decode(String.self, forKey: .gender)
gender = Gender.genderForString(genderStr)
birthYear = try container.decode(String.self, forKey: .birthYear)
let heightStr = try container.decode(String.self, forKey: .height)
height = Int(heightStr) ?? 0
let massStr = try container.decode(String.self, forKey: .mass)
mass = Int(massStr) ?? 0
hairColor = try container.decode(String.self, forKey: .hairColor)
skinColor = try container.decode(String.self, forKey: .skinColor)
eyeColor = try container.decode(String.self, forKey: .eyeColor)
}
}
struct PersonSearch: Decodable {
let results: [Person]
}
Hummmmmmm
If that’s not enough for you, I suppose you can have the rest of the project. It’s not much, but it’s fun!