Skip to content

Commit ae97945

Browse files
committed
update to 6.1 final
1 parent 6e05d31 commit ae97945

File tree

8 files changed

+56
-57
lines changed

8 files changed

+56
-57
lines changed

bk2ch24p834simpleHTTP/ch37p1088simpleHTTP/Base.lproj/Main.storyboard

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.8" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
55
</dependencies>
66
<scenes>
77
<!--View Controller-->
@@ -42,9 +42,6 @@
4242
<simulatedMetricsContainer key="defaultSimulatedMetrics">
4343
<simulatedStatusBarMetrics key="statusBar"/>
4444
<simulatedOrientationMetrics key="orientation"/>
45-
<simulatedScreenMetrics key="destination" type="retina4">
46-
<size key="portraitSize" width="320" height="568"/>
47-
<size key="landscapeSize" width="568" height="320"/>
48-
</simulatedScreenMetrics>
45+
<simulatedScreenMetrics key="destination" type="retina4"/>
4946
</simulatedMetricsContainer>
5047
</document>

bk2ch24p834simpleHTTP/ch37p1088simpleHTTP/ViewController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ class ViewController: UIViewController {
99
@IBAction func doSimpleHTTP (sender:AnyObject!) {
1010
self.iv.image = nil
1111
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
12-
let url = NSURL(string:s)
12+
let url = NSURL(string:s)!
1313
let session = NSURLSession.sharedSession()
1414
let task = session.downloadTaskWithURL(url) {
1515
(loc:NSURL!, response:NSURLResponse!, error:NSError!) in
16-
println("here")
17-
if error != nil {
18-
println(error)
19-
return
20-
}
16+
println("here")
17+
if error != nil {
18+
println(error)
19+
return
20+
}
2121
let status = (response as NSHTTPURLResponse).statusCode
2222
println("response status: \(status)")
2323
if status != 200 {
2424
println("oh well")
2525
return
2626
}
27-
let d = NSData(contentsOfURL:loc)
27+
let d = NSData(contentsOfURL:loc)!
2828
let im = UIImage(data:d)
2929
dispatch_async(dispatch_get_main_queue()) {
3030
self.iv.image = im

bk2ch24p837lessSimpleHTTP/ch37p1089lessSimpleHTTP/ViewController.swift

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class ViewController: UIViewController, NSURLSessionDownloadDelegate {
77
@IBOutlet var iv : UIImageView!
88
var task : NSURLSessionTask!
99

10+
let which = 1
11+
1012
lazy var session : NSURLSession = {
1113
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
1214
config.allowsCellularAccess = false
@@ -22,29 +24,38 @@ class ViewController: UIViewController, NSURLSessionDownloadDelegate {
2224
}
2325

2426
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
25-
let url = NSURL(string:s)
27+
let url = NSURL(string:s)!
2628
let req = NSMutableURLRequest(URL:url)
27-
NSURLProtocol.setProperty("howdy", forKey:"greeting", inRequest:req)
29+
if which == 1 { // show how to attach stuff to the request
30+
NSURLProtocol.setProperty("howdy", forKey:"greeting", inRequest:req)
31+
}
2832
let task = self.session.downloadTaskWithRequest(req)
2933
self.task = task
3034
self.iv.image = nil
3135
task.resume()
3236

3337
}
34-
3538

36-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
37-
println("downloaded \(100*totalBytesWritten/totalBytesExpectedToWrite)%")
39+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
40+
println("downloaded \(100*writ/exp)%")
3841
}
3942

40-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
43+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
4144
// unused in this example
4245
}
4346

44-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
45-
let req = downloadTask.originalRequest
46-
if let greeting = NSURLProtocol.propertyForKey("greeting", inRequest:req) as? String {
47-
println(greeting)
47+
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
48+
println("completed: error: \(error)")
49+
}
50+
51+
// this is the only required NSURLSessionDownloadDelegate method
52+
53+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
54+
if which == 1 {
55+
let req = downloadTask.originalRequest
56+
if let greeting = NSURLProtocol.propertyForKey("greeting", inRequest:req) as? String {
57+
println(greeting)
58+
}
4859
}
4960
self.task = nil
5061
let response = downloadTask.response as NSHTTPURLResponse
@@ -53,17 +64,13 @@ class ViewController: UIViewController, NSURLSessionDownloadDelegate {
5364
if stat != 200 {
5465
return
5566
}
56-
let d = NSData(contentsOfURL:location)
67+
let d = NSData(contentsOfURL:location)!
5768
let im = UIImage(data:d)
5869
dispatch_async(dispatch_get_main_queue()) {
5970
self.iv.image = im
6071
}
6172
}
6273

63-
func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didCompleteWithError error: NSError!) {
64-
println("completed: error: \(error)")
65-
}
66-
6774
override func viewWillDisappear(animated: Bool) {
6875
super.viewWillDisappear(animated)
6976
self.session.finishTasksAndInvalidate()

bk2ch24p838lessSimpleHTTP2/DataTaskExample/Base.lproj/Main.storyboard

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6206.8" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="GKS-bV-ifY">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="GKS-bV-ifY">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
55
</dependencies>
66
<scenes>
77
<!--Navigation Controller-->
@@ -85,9 +85,6 @@
8585
<simulatedMetricsContainer key="defaultSimulatedMetrics">
8686
<simulatedStatusBarMetrics key="statusBar"/>
8787
<simulatedOrientationMetrics key="orientation"/>
88-
<simulatedScreenMetrics key="destination" type="retina4">
89-
<size key="portraitSize" width="320" height="568"/>
90-
<size key="landscapeSize" width="568" height="320"/>
91-
</simulatedScreenMetrics>
88+
<simulatedScreenMetrics key="destination" type="retina4"/>
9289
</simulatedMetricsContainer>
9390
</document>

bk2ch24p838lessSimpleHTTP2/DataTaskExample/ViewController.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
Not in the book. But this seems like a serious omission;
3-
I should demonstrate minimally how to do a data task, as well as a download task.
4-
*/
51

62

73
import UIKit
@@ -25,7 +21,7 @@ class ViewController: UIViewController, NSURLSessionDataDelegate {
2521
}
2622

2723
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
28-
let url = NSURL(string:s)
24+
let url = NSURL(string:s)!
2925
let req = NSMutableURLRequest(URL:url)
3026
let task = self.session.dataTaskWithRequest(req) // *
3127
self.task = task
@@ -35,14 +31,14 @@ class ViewController: UIViewController, NSURLSessionDataDelegate {
3531

3632
}
3733

38-
func URLSession(session: NSURLSession!, dataTask: NSURLSessionDataTask!, didReceiveData data: NSData!) {
34+
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
3935
println("received \(data.length) bytes of data")
4036
// do something with the data here!
4137
self.data.appendData(data)
4238
}
4339

4440

45-
func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didCompleteWithError error: NSError!) {
41+
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
4642
println("completed: error: \(error)")
4743
self.task = nil
4844
if error == nil {

bk2ch24p842downloader/ch37p1099downloader/MyDownloader.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MyDownloader: NSObject, NSURLSessionDownloadDelegate {
2727
}
2828

2929
func download(s:String, completionHandler ch : MyDownloaderCompletionHandler) -> NSURLSessionTask {
30-
let url = NSURL(string:s)
30+
let url = NSURL(string:s)!
3131
let req = NSMutableURLRequest(URL:url)
3232
let ch2 : AnyObject = unsafeBitCast(ch, AnyObject.self)
3333
NSURLProtocol.setProperty(ch2, forKey:"ch", inRequest:req)
@@ -36,18 +36,18 @@ class MyDownloader: NSObject, NSURLSessionDownloadDelegate {
3636
return task
3737
}
3838

39-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
40-
println("downloaded \(100*totalBytesWritten/totalBytesExpectedToWrite)%")
39+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
40+
println("downloaded \(100*writ/exp)%")
4141
}
4242

43-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
43+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
4444
// unused in this example
4545
println("did resume")
4646
}
4747

48-
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
48+
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
4949
let req = downloadTask.originalRequest
50-
let ch : AnyObject = NSURLProtocol.propertyForKey("ch", inRequest:req)
50+
let ch : AnyObject = NSURLProtocol.propertyForKey("ch", inRequest:req)!
5151
let response = downloadTask.response as NSHTTPURLResponse
5252
let stat = response.statusCode
5353
println("status \(stat)")

bk2ch24p842downloader/ch37p1099downloader/MyTableViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class MyTableViewController: UITableViewController {
4141
return arr
4242
}()
4343

44-
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
44+
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
4545
return 1
4646
}
4747

48-
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
48+
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
4949
return self.model.count
5050
}
5151

52-
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
52+
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
5353
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as UITableViewCell
5454
let m = self.model[indexPath.row]
5555
cell.textLabel.text = m.text
@@ -65,7 +65,7 @@ class MyTableViewController: UITableViewController {
6565
if url == nil {
6666
return
6767
}
68-
let data = NSData(contentsOfURL: url)
68+
let data = NSData(contentsOfURL: url)!
6969
let im = UIImage(data:data)
7070
m.im = im
7171
dispatch_async(dispatch_get_main_queue()) {
@@ -77,7 +77,7 @@ class MyTableViewController: UITableViewController {
7777
return cell
7878
}
7979

80-
override func tableView(tableView: UITableView!, didEndDisplayingCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
80+
override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
8181
let m = self.model[indexPath.row]
8282
if let task = m.task {
8383
if task.state == .Running {

bk2ch24p842downloader/ch37p1099downloader/ViewController.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ViewController: UIViewController {
1414
}()
1515

1616
lazy var downloader : MyDownloader = {
17-
return MyDownloader(configuration:self.configuration)
17+
let d : MyDownloader = MyDownloader(configuration:self.configuration)
18+
return d
1819
}()
1920

2021
@IBAction func doDownload (sender:AnyObject!) {
@@ -25,10 +26,11 @@ class ViewController: UIViewController {
2526
if url == nil {
2627
return
2728
}
28-
let d = NSData(contentsOfURL: url)
29-
let im = UIImage(data:d)
30-
dispatch_async(dispatch_get_main_queue()) {
31-
self.iv.image = im
29+
if let d = NSData(contentsOfURL: url) {
30+
let im = UIImage(data:d)
31+
dispatch_async(dispatch_get_main_queue()) {
32+
self.iv.image = im
33+
}
3234
}
3335
}
3436
}

0 commit comments

Comments
 (0)