Skip to content

Instantly share code, notes, and snippets.

@jquave
Created August 10, 2014 01:41
Show Gist options
  • Select an option

  • Save jquave/3c355c017071889f522d to your computer and use it in GitHub Desktop.

Select an option

Save jquave/3c355c017071889f522d to your computer and use it in GitHub Desktop.
class func albumsWithJSON(allResults: NSArray) -> [Album] {
// Create an empty array of Albums to append to from this list
var albums = [Album]()
// Store the results in our table data array
if allResults.count>0 {
// Sometimes iTunes returns a collection, not a track, so we check both for the 'name'
for result in allResults {
var name = (result["trackName"] as? String) ?? (result["collectionName"] as? String) ?? "Untitled"
var priceVal = result["collectionPrice"] as? Float ?? 0.0
var price = "$???"
var nf: NSNumberFormatter = NSNumberFormatter()
nf.maximumFractionDigits = 2;
if priceVal != nil {
price = "$"+nf.stringFromNumber(priceVal)
}
let thumbnailURL = result["artworkUrl60"] as? String ?? ""
let imageURL = result["artworkUrl100"] as? String ?? ""
let artistURL = result["artistViewUrl"] as? String ?? "Unknown"
var itemURL = (result["collectionViewUrl"] as? String) ?? (result["trackViewUrl"] as? String) ?? ""
var collectionId = result["collectionId"] as? Int ?? 0
var newAlbum = Album(name: name, price: price, thumbnailImageURL: thumbnailURL, largeImageURL: imageURL, itemURL: itemURL, artistURL: artistURL, collectionId: collectionId)
albums.append(newAlbum)
}
}
return albums
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment