Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created April 5, 2017 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pasanpr/659f04904bfffb5143762fb0161cf28b to your computer and use it in GitHub Desktop.
Save Pasanpr/659f04904bfffb5143762fb0161cf28b to your computer and use it in GitHub Desktop.
extension Song {
init?(json: [String: Any]) {
struct Key {
static let id = "trackId"
static let name = "trackName"
static let censoredName = "trackCensoredName"
static let trackTime = "trackTimeMillis"
static let isExplicit = "trackExplicitness"
}
guard let idValue = json[Key.id] as? Int,
let nameValue = json[Key.name] as? String,
let censoredNameValue = json[Key.censoredName] as? String,
let trackTimeValue = json[Key.trackTime] as? Int,
let isExplicitString = json[Key.isExplicit] as? String else {
return nil
}
self.id = idValue
self.name = nameValue
self.censoredName = censoredNameValue
self.trackTime = trackTimeValue
self.isExplicit = isExplicitString == "notExplicit" ? false : true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment