Skip to content

Commit 3be55d6

Browse files
committed
Add get function to API controller, and add collectionID to Album model.
1 parent caf67fa commit 3be55d6

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

TestSwift.xcodeproj/project.pbxproj

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@
6363
/* End PBXFrameworksBuildPhase section */
6464

6565
/* Begin PBXGroup section */
66+
021E1AEA1944CC8300FDC37C /* Views */ = {
67+
isa = PBXGroup;
68+
children = (
69+
02CCF4A9193D16930069AEE9 /* SearchResultsViewController.swift */,
70+
022889EE193FC3AC00D729A0 /* DetailsViewController.swift */,
71+
02CCF4AB193D16930069AEE9 /* Main.storyboard */,
72+
);
73+
name = Views;
74+
sourceTree = "<group>";
75+
};
76+
021E1AEB1944CC8D00FDC37C /* Model */ = {
77+
isa = PBXGroup;
78+
children = (
79+
022889F0193FD6C000D729A0 /* Album.swift */,
80+
);
81+
name = Model;
82+
sourceTree = "<group>";
83+
};
84+
021E1AEC1944CC9500FDC37C /* REST */ = {
85+
isa = PBXGroup;
86+
children = (
87+
02F504DA193E0B630037F599 /* APIController.swift */,
88+
);
89+
name = REST;
90+
sourceTree = "<group>";
91+
};
92+
021E1AED1944CCA800FDC37C /* Graphics */ = {
93+
isa = PBXGroup;
94+
children = (
95+
0229AE62193F97EF00BCED97 /* Blank52.png */,
96+
);
97+
name = Graphics;
98+
sourceTree = "<group>";
99+
};
66100
02CCF499193D16930069AEE9 = {
67101
isa = PBXGroup;
68102
children = (
@@ -84,13 +118,10 @@
84118
02CCF4A4193D16930069AEE9 /* TestSwift */ = {
85119
isa = PBXGroup;
86120
children = (
87-
0229AE62193F97EF00BCED97 /* Blank52.png */,
88-
022889F0193FD6C000D729A0 /* Album.swift */,
89-
02CCF4A7193D16930069AEE9 /* AppDelegate.swift */,
90-
02CCF4A9193D16930069AEE9 /* SearchResultsViewController.swift */,
91-
02F504DA193E0B630037F599 /* APIController.swift */,
92-
022889EE193FC3AC00D729A0 /* DetailsViewController.swift */,
93-
02CCF4AB193D16930069AEE9 /* Main.storyboard */,
121+
021E1AED1944CCA800FDC37C /* Graphics */,
122+
021E1AEC1944CC9500FDC37C /* REST */,
123+
021E1AEB1944CC8D00FDC37C /* Model */,
124+
021E1AEA1944CC8300FDC37C /* Views */,
94125
02CCF4AE193D16930069AEE9 /* Images.xcassets */,
95126
02CCF4A5193D16930069AEE9 /* Supporting Files */,
96127
028D579A1944A6C300268B8F /* TestSwift-Bridging-Header.h */,
@@ -101,6 +132,7 @@
101132
02CCF4A5193D16930069AEE9 /* Supporting Files */ = {
102133
isa = PBXGroup;
103134
children = (
135+
02CCF4A7193D16930069AEE9 /* AppDelegate.swift */,
104136
02CCF4A6193D16930069AEE9 /* Info.plist */,
105137
);
106138
name = "Supporting Files";

TestSwift/APIController.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ class APIController {
2020
self.delegate = delegate
2121
}
2222

23-
func searchItunesFor(searchTerm: String) {
24-
25-
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
26-
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
27-
28-
// Now escape anything else that isn't URL-friendly
29-
let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
30-
let urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album"
31-
let url = NSURL(string: urlPath)
23+
func get(path: String) {
24+
let url = NSURL(string: path)
3225
let request = NSURLRequest(URL: url)
33-
26+
3427
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
3528
if error? {
3629
println("ERROR: \(error.localizedDescription)")
@@ -49,4 +42,19 @@ class APIController {
4942
}
5043
})
5144
}
45+
46+
func searchItunesFor(searchTerm: String) {
47+
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
48+
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
49+
50+
// Now escape anything else that isn't URL-friendly
51+
let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
52+
let urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album"
53+
get(urlPath)
54+
}
55+
56+
func lookupTrackWithID(trackID: String) {
57+
58+
}
59+
5260
}

TestSwift/Classes/Album.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ class Album {
1414
var largeImageURL: String?
1515
var itemURL: String?
1616
var artistURL: String?
17+
var collectionID: Int?
1718

18-
init(name: String!, price: String!, thumbnailImageURL: String!, largeImageURL: String!, itemURL: String!, artistURL: String!) {
19+
init(name: String!, price: String!, thumbnailImageURL: String!, largeImageURL: String!, itemURL: String!, artistURL: String!, collectionID: Int?) {
1920
self.title = name
2021
self.price = price
2122
self.thumbnailImageURL = thumbnailImageURL
2223
self.largeImageURL = largeImageURL
2324
self.itemURL = itemURL
2425
self.artistURL = artistURL
26+
self.collectionID = collectionID
2527
}
2628
}

TestSwift/SearchResultsViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
136136
itemURL = result["trackViewUrl"] as? String
137137
}
138138

139-
var newAlbum = Album(name: name!, price: price!, thumbnailImageURL: thumbnailURL!, largeImageURL: imageURL!, itemURL: itemURL!, artistURL: artistURL!)
139+
var collectionID = result["collectionId"] as? Int
140+
141+
var newAlbum = Album(name: name!, price: price!, thumbnailImageURL: thumbnailURL!, largeImageURL: imageURL!, itemURL: itemURL!, artistURL: artistURL!, collectionID: collectionID!)
140142
albums.append(newAlbum)
141143
}
142144

0 commit comments

Comments
 (0)