Skip to content

Commit 8f7917c

Browse files
committed
update for Swift 1.2, Xcode 6.3
1 parent d4c585e commit 8f7917c

File tree

6 files changed

+10
-39
lines changed

6 files changed

+10
-39
lines changed

bk2ch02p052mars/ch15p406mars/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ViewController : UIViewController {
6767
delay (2) {
6868
iv.image = UIImage(named:"bottle5.png")
6969
// if we're using constraints...
70-
// the image view is resized setting the image changes the intrinsic content size
70+
// the image view is resized, because setting the image changes the intrinsic content size
7171
}
7272

7373
}

bk2ch02p057imageRendering/imageRendering/ViewController.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ class ViewController : UIViewController {
77
override func viewDidLoad() {
88
super.viewDidLoad()
99

10-
/*
11-
Very cool. In seed 5, Swift understands this correctly as needing a double unwrap.
12-
I have not said what class the delegate is, and the window var is optional,
13-
so we don't know the getter is even implemented.
14-
So we must unwrap twice: once because we think there is a window var,
15-
and again because we think there is a real UIWindow there.
16-
*/
17-
18-
/*
19-
Very uncool. Now (seed 7) they've gone too far.
20-
Not only is delegate itself now an optional that needs unwrapping, but
21-
window must be unwrapped twice.
22-
*/
2310

2411
if let window = UIApplication.sharedApplication().delegate!.window! {
2512
window.tintColor = UIColor.redColor()

bk2ch02p059sixBlueCircles/ch15p411sixBlueCircles/Views.swift

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyImageView2 : UIImageView {
5757
// let im = UIGraphicsGetImageFromCurrentImageContext()
5858
// UIGraphicsEndImageContext()
5959

60-
self.image = drawnImage()(CGSizeMake(100,100)) {
60+
self.image = imageOfSize(CGSizeMake(100,100)) {
6161
let con = UIGraphicsGetCurrentContext()
6262
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100))
6363
CGContextSetFillColorWithColor(con, UIColor.blueColor().CGColor)
@@ -78,28 +78,11 @@ UIGraphicsEndImageContext()
7878
Since the purpose is to extract the image, it would be nice to replace that with a functional architecture that clearly yields the image. Moreover, such an architecture has the advantage of isolating any local variables used within the "sandwich". In Objective-C you can at least wrap the interior in curly braces to form a scope, but Swift, with its easy closure formation, offers the opportunity for an even clearer presentation, along these lines:
7979
*/
8080

81-
func imageOfSize(size:CGSize, closure:() -> ()) -> UIImage {
82-
UIGraphicsBeginImageContextWithOptions(size, false, 0)
81+
func imageOfSize(size:CGSize, opaque:Bool = false, closure:() -> ()) -> UIImage {
82+
UIGraphicsBeginImageContextWithOptions(size, opaque, 0)
8383
closure()
8484
let result = UIGraphicsGetImageFromCurrentImageContext()
8585
UIGraphicsEndImageContext()
8686
return result
8787
}
8888

89-
/*
90-
In the above, I've assumed that the context is not opaque (second parameter is false). If we want to specify that explicitly, we can, but it would be nice to have to specify it always - we'd like the opaque parameter to be optional. But you can't have an optional second parameter and a third closure parameter. The solution is to curry the function into two sets of parameters:
91-
*/
92-
93-
func drawnImage (opaque:Bool = false) -> (CGSize, () -> ()) -> UIImage {
94-
func imageOfSize(size:CGSize, closure:() -> ()) -> UIImage {
95-
UIGraphicsBeginImageContextWithOptions(size, opaque, 0)
96-
closure()
97-
let result = UIGraphicsGetImageFromCurrentImageContext()
98-
UIGraphicsEndImageContext()
99-
return result
100-
}
101-
return imageOfSize
102-
}
103-
104-
105-

bk2ch02p070filters/ch15p419filters/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ViewController : UIViewController {
3333

3434
// two ways to obtain final bitmap
3535

36-
let which = 2
36+
let which = 1
3737
switch which {
3838
case 1:
3939
let moicg = CIContext(options: nil).createCGImage(blendimage, fromRect: moiextent)

bk2ch02p071BlurAndVibrancy/BlurAndVibrancy/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ViewController: UIViewController {
2222
blur.frame = mainview.bounds
2323
blur.autoresizingMask = .FlexibleWidth | .FlexibleHeight
2424
let vib = UIVisualEffectView(effect: UIVibrancyEffect(
25-
forBlurEffect: blur.effect as UIBlurEffect))
25+
forBlurEffect: blur.effect as! UIBlurEffect))
2626
let lab = UILabel()
2727
lab.text = "Hello, world!"
2828
lab.sizeToFit()

bk2ch02p077drawingInUIView/ch15p427drawingInUIView/MyView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import UIKit
33

44
class MyView : UIView {
55

6-
override init() {
6+
init() {
77
super.init(frame:CGRectZero)
88
self.opaque = false
99
}
1010

1111
required init(coder: NSCoder) {
12-
fatalError("NSCoding not supported")
12+
super.init(coder: coder)
13+
self.opaque = false
1314
}
1415

1516
override init(frame: CGRect) {
@@ -18,7 +19,7 @@ class MyView : UIView {
1819
}
1920

2021
override func drawRect(rect: CGRect) {
21-
let which = 6
22+
let which = 1
2223
switch which {
2324
case 1:
2425
let con = UIGraphicsGetCurrentContext()

0 commit comments

Comments
 (0)