Skip to content

Instantly share code, notes, and snippets.

@Pasanpr Pasanpr/Album.swift Secret
Last active May 12, 2017

What would you like to do?
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[] as? Int,
let artistNameValue = json[Key.artistName] as? String,
let nameValue = json[] 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 = 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
You can’t perform that action at this time.