-
-
Save Pasanpr/629eb57f4e9b610285092255b41e537b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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