-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathAlbum.swift
More file actions
79 lines (64 loc) · 2.93 KB
/
Album.swift
File metadata and controls
79 lines (64 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Album.swift
// MusicPlayer
//
// Created by Jameson Quave on 9/16/14.
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
//
import Foundation
class Album {
var title: String
var price: String
var thumbnailImageURL: String
var largeImageURL: String
var itemURL: String
var artistURL: String
var collectionId: Int
init(name: String, price: String, thumbnailImageURL: String, largeImageURL: String, itemURL: String, artistURL: String, collectionId: Int) {
self.title = name
self.price = price
self.thumbnailImageURL = thumbnailImageURL
self.largeImageURL = largeImageURL
self.itemURL = itemURL
self.artistURL = artistURL
self.collectionId = collectionId
}
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
if name == nil {
name = result["collectionName"] as? String
}
// Sometimes price comes in as formattedPrice, sometimes as collectionPrice.. and sometimes it's a float instead of a string. Hooray!
var price = result["formattedPrice"] as? String
if price == nil {
price = result["collectionPrice"] as? String
if price == nil {
var priceFloat: Float? = result["collectionPrice"] as? Float
var nf: NSNumberFormatter = NSNumberFormatter()
nf.maximumFractionDigits = 2
if priceFloat != nil {
price = "$"+nf.stringFromNumber(priceFloat!)!
}
}
}
let thumbnailURL = result["artworkUrl60"] as? String ?? ""
let imageURL = result["artworkUrl100"] as? String ?? ""
let artistURL = result["artistViewUrl"] as? String ?? ""
var itemURL = result["collectionViewUrl"] as? String
if itemURL == nil {
itemURL = result["trackViewUrl"] as? String
}
var collectionId = result["collectionId"] as? Int
var newAlbum = Album(name: name!, price: price!, thumbnailImageURL: thumbnailURL, largeImageURL: imageURL, itemURL: itemURL!, artistURL: artistURL, collectionId: collectionId!)
albums.append(newAlbum)
}
}
return albums
}
}