Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Last active May 12, 2017 04:24
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/629eb57f4e9b610285092255b41e537b to your computer and use it in GitHub Desktop.
Save Pasanpr/629eb57f4e9b610285092255b41e537b to your computer and use it in GitHub Desktop.
extension Album {
convenience init?(json: [String: Any]) {
struct Key {
static let id = "collectionId"
static let artistName = "artistName"
static let name = "collectionName"
static let censoredName = "collectionCensoredName"
static let artworkUrl = "artworkUrl100"
static let collectionExplicitness = "collectionExplicitness"
static let trackCount = "trackCount"
static let releaseDate = "releaseDate"
static let primaryGenre = "primaryGenreName"
}
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
guard let idValue = json[Key.id] as? Int,
let artistNameValue = json[Key.artistName] as? String,
let nameValue = json[Key.name] as? String,
let censoredNameValue = json[Key.censoredName] as? String,
let artworkUrlString = json[Key.artworkUrl] as? String,
let isExplicitValue = json[Key.collectionExplicitness] as? String,
let numberOfTracksValue = json[Key.trackCount] as? Int,
let releaseDateString = json[Key.releaseDate] as? String,
let releaseDateValue = formatter.date(from: releaseDateString),
let primaryGenreString = json[Key.primaryGenre] as? String,
let primaryGenreValue = Genre(name: primaryGenreString) else { return nil }
let isExplicit = isExplicitValue == "notExplicit" ? false : true
self.init(id: idValue, artistName: artistNameValue, name: nameValue, censoredName: censoredNameValue, artworkUrl: artworkUrlString, isExplicit: isExplicit, numberOfTracks: numberOfTracksValue, releaseDate: releaseDateValue, primaryGenre: primaryGenreValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment