Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Moves JSON parsing/logic for "album" to the Album model
init(json: Dictionary<String, AnyObject>){
var name: String? = json[ "trackName" ] as AnyObject? as? String
if !name? {
name = json[ "collectionName" ] as AnyObject? as? String
}
//Sometimes price comes in as formattedPrice, sometimes as collectionPrice.. and sometimes it's a float instead of a string. Hooray!
var price: String? = json["formattedPrice"] as AnyObject? as? String
if !price? {
price = json[ "collectionPrice" ] as AnyObject? as? String
if !price? {
var priceFloat: Float? = json[ "collectionPrice" ] as AnyObject? as? Float
var nf = NSNumberFormatter()
nf.maximumFractionDigits = 2;
if priceFloat? {
price = "$" + nf.stringFromNumber(priceFloat!)
}
}
}
let thumbnailURL: String? = json["artworkUrl60"] as AnyObject? as? String
let imageURL: String? = json["artworkUrl100"] as AnyObject? as? String
let artistURL: String? = json["artistViewUrl"] as AnyObject? as? String
var itemURL: String? = json["collectionViewUrl"] as AnyObject? as? String
if !itemURL? {
itemURL = json["trackViewUrl"] as AnyObject? as? String
}
self.title = name!
self.price = price!
self.thumbnailImageURL = thumbnailURL!
self.largeImageURL = imageURL!
self.itemURL = itemURL!
self.artistURL = artistURL!
}
func didReceiveAPIResults(results: NSDictionary){
// Store results in our table data array
if results.count > 0{
if let dict = results as NSDictionary? {
if let allResults = dict["results"] as? Array<Dictionary<String,AnyObject>>{
for item in allResults{
albums.append(Album(json: item))
}
}
}
dispatch_async(dispatch_get_main_queue(), {
self.appsTableView.reloadData()
})
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
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.