Skip to content

Instantly share code, notes, and snippets.

@frankieshakes
Created July 24, 2014 22:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save frankieshakes/dc431d816e47e391f83e to your computer and use it in GitHub Desktop.
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