Skip to content

Commit a5f311a

Browse files
committed
Part 6
1 parent 0f5830e commit a5f311a

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

TestSwift/APIController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ protocol APIControllerProtocol {
1414

1515
class APIController: NSObject {
1616

17-
var data: NSMutableData = NSMutableData()
17+
let data: NSMutableData = NSMutableData()
1818
var delegate: APIControllerProtocol?
1919

2020
func searchItunesFor(searchTerm: String) {
2121

2222
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
23-
var itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
23+
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
2424

2525
// Now escape anything else that isn't URL-friendly
26-
var escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
27-
var urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album"
28-
var url: NSURL = NSURL(string: urlPath)
29-
var request: NSURLRequest = NSURLRequest(URL: url)
26+
let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
27+
let urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album"
28+
let url: NSURL = NSURL(string: urlPath)
29+
let request: NSURLRequest = NSURLRequest(URL: url)
3030

3131
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
3232
if error? {
3333
println("ERROR: \(error.localizedDescription)")
3434
}
3535
else {
3636
var error: NSError?
37-
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
37+
let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
3838
// Now send the JSON result to our delegate object
3939
if error? {
4040
println("HTTP Error: \(error?.localizedDescription)")

TestSwift/SearchResultsViewController.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ class SearchResultsViewController: UIViewController,/* UITableViewDataSource, UI
1515
var api: APIController = APIController()
1616
@IBOutlet var appsTableView : UITableView
1717

18-
var tableData: Dictionary<String, AnyObject>[] = []
1918
var albums: Album[] = []
20-
21-
var imageCache = NSMutableDictionary()
19+
//var imageCache = NSMutableDictionary()
20+
var imageCache = Dictionary<String, UIImage>()
2221

2322
override func viewDidLoad() {
2423
super.viewDidLoad()
@@ -48,25 +47,16 @@ class SearchResultsViewController: UIViewController,/* UITableViewDataSource, UI
4847
}
4948

5049
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
51-
//return countElements(self.tableData)
52-
//return self.tableData.count
5350
return albums.count
5451
}
5552

5653

5754
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
5855

59-
60-
6156
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
62-
if cell == nil {
63-
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)
64-
}
6557

66-
var index: Int = indexPath.row
67-
68-
// Find this cell's album by passing in the Int 'index' to the subscript method for an array of type Album[]
69-
var album = self.albums[index]
58+
// Find this cell's album by passing in the indexPath.row to the subscript method for an array of type Album[]
59+
let album = self.albums[indexPath.row]
7060

7161
cell.text = album.title
7262
cell.image = UIImage(named: "Blank52")
@@ -78,24 +68,24 @@ class SearchResultsViewController: UIViewController,/* UITableViewDataSource, UI
7868

7969
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
8070
//var urlString: NSString = rowData["artworkUrl60"] as NSString
81-
var urlString = album.thumbnailImageURL
71+
let urlString = album.thumbnailImageURL
8272

8373
// Check our image cache for the existing key. This is just a dictionary of UIImages
84-
var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
74+
var image: UIImage? = self.imageCache[urlString!]
8575

8676
if( !image? ) {
8777
// If the image does not exist, we need to download it
88-
var imgURL: NSURL = NSURL(string: urlString)
78+
let imgURL: NSURL = NSURL(string: urlString)
8979

9080
// Download an NSData representation of the image at the URL
91-
var request: NSURLRequest = NSURLRequest(URL: imgURL)
92-
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)
81+
let request: NSURLRequest = NSURLRequest(URL: imgURL)
82+
let urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)
9383
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
9484
if !error? {
9585
image = UIImage(data: data)
9686

9787
// Store the image in to our cache
98-
self.imageCache.setValue(image, forKey: urlString)
88+
self.imageCache[urlString!] = image
9989

10090
// Sometimes this request takes a while, and it's possible that a cell could be re-used before the art is done loading.
10191
// Let's explicitly call the cellForRowAtIndexPath method of our tableView to make sure the cell is not nil, and therefore still showing onscreen.
@@ -125,7 +115,7 @@ class SearchResultsViewController: UIViewController,/* UITableViewDataSource, UI
125115
// Store the results in our table data array
126116
if results.count>0 {
127117

128-
var allResults: NSDictionary[] = results["results"] as NSDictionary[]
118+
let allResults: NSDictionary[] = results["results"] as NSDictionary[]
129119

130120
// var swiftResultArray: NSDictionary[] = allResults
131121

@@ -152,10 +142,9 @@ class SearchResultsViewController: UIViewController,/* UITableViewDataSource, UI
152142
}
153143
}
154144

155-
var thumbnailURL: String? = result["artworkUrl60"] as? String
156-
var imageURL: String? = result["artworkUrl100"] as? String
157-
158-
var artistURL: String? = result["artistViewUrl"] as? String
145+
let thumbnailURL: String? = result["artworkUrl60"] as? String
146+
let imageURL: String? = result["artworkUrl100"] as? String
147+
let artistURL: String? = result["artistViewUrl"] as? String
159148

160149
var itemURL: String? = result["collectionViewUrl"] as? String
161150
if !itemURL? {

0 commit comments

Comments
 (0)