Skip to content

Commit 692f73d

Browse files
committed
inject some missing examples from chapter 10; correct Locale.current throughout; use #keyPath more
1 parent 05d2adc commit 692f73d

File tree

12 files changed

+78
-41
lines changed

12 files changed

+78
-41
lines changed

bk1ch10p428foundationClasses/bk1ch10p428foundationClasses/ViewController.swift

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ class ViewController: UIViewController {
3232
}
3333

3434
do {
35-
let r = NSRange(Range<Int>(1...3))
35+
// let rr = NSRange(1...3)
36+
let r = NSRange(Range(1...3))
3637
print(r)
38+
debugPrint(r) // that didn't help
39+
print(NSStringFromRange(r))
3740
let r2 = r.toRange()
3841
print(r2)
3942
// let r3 = Range(r)
@@ -107,7 +110,7 @@ class ViewController: UIViewController {
107110
let d = comp.date // Optional wrapping Date
108111
if let d = d {
109112
print(d)
110-
print(d.description(with:Locale.current()))
113+
print(d.description(with:Locale.current))
111114
}
112115

113116
}
@@ -120,11 +123,23 @@ class ViewController: UIViewController {
120123
print("one month from now:", d2)
121124
}
122125

126+
do {
127+
let greg = Calendar(calendarIdentifier:.gregorian)!
128+
let d1 = DateComponents(calendar: greg,
129+
year: 2016, month: 1, day: 1, hour: 0).date!
130+
let d2 = DateComponents(calendar: greg,
131+
year: 2016, month: 8, day: 10, hour: 15).date!
132+
let di = DateInterval(start: d1, end: d2)
133+
if di.contains(Date()) { // are we currently between those two dates?
134+
print("yep")
135+
}
136+
}
137+
123138
do {
124139
let df = DateFormatter()
125140
let format = DateFormatter.dateFormat(
126141
fromTemplate:"dMMMMyyyyhmmaz", options:0,
127-
locale:Locale.current())
142+
locale:Locale.current)
128143
df.dateFormat = format
129144
let s = df.string(from: Date()) // just now
130145
print(s)
@@ -187,6 +202,19 @@ class ViewController: UIViewController {
187202
ud.set(cdata, forKey: "myColor")
188203
}
189204

205+
do {
206+
let m1 = Measurement(value:5, unit: UnitLength.miles)
207+
let m2 = Measurement(value:6, unit: UnitLength.kilometers)
208+
let total = m1 + m2
209+
print(total)
210+
let totalFeet = total.converted(to: .feet).value // 46084.9737532808
211+
print(totalFeet)
212+
let total2 = Measurement<Unit>(value:total.value, unit:total.unit)
213+
let mf = MeasurementFormatter()
214+
let s = mf.string(from:total2) // "8.728 mi"
215+
print(s)
216+
}
217+
190218
do {
191219
let n1 = NSNumber(value:1)
192220
let n2 = NSNumber(value:2)
@@ -211,10 +239,11 @@ class ViewController: UIViewController {
211239
}
212240

213241
do {
214-
let arr = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
242+
let arr = ["zero", "one", "two", "three", "four", "five",
243+
"six", "seven", "eight", "nine", "ten"]
215244
var ixs = IndexSet()
216-
ixs.insert(integersIn: 1..<5)
217-
ixs.insert(integersIn: 8..<11)
245+
ixs.insert(integersIn: Range(1...4))
246+
ixs.insert(integersIn: Range(8...10))
218247
let arr2 = (arr as NSArray).objects(at:ixs)
219248
print(arr2)
220249
}
@@ -230,7 +259,7 @@ class ViewController: UIViewController {
230259
// filed a bug: "ambiguous" without the []
231260

232261
let ems = pep.objects(
233-
at: pep.indexesOfObjects([]) {
262+
at: pep.indexesOfObjects(options:[]) {
234263
(obj, idx, stop) -> Bool in
235264
return (obj as! NSString).range(
236265
of: "m", options:.caseInsensitive

bk2ch04p134springing/ch17p486springing/ViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ViewController : UIViewController {
1919
// new in iOS 9, springing is exposed at layer level
2020
CATransaction.setDisableActions(true)
2121
self.v.layer.position.y += 100
22-
let anim = CASpringAnimation(keyPath: "position")
22+
// note to self, need to use #keyPath throughout
23+
let anim = CASpringAnimation(keyPath: #keyPath(CALayer.position))
2324
anim.damping = 0.7
2425
anim.initialVelocity = 20
2526
anim.mass = 0.04

bk2ch10p507tabStops/TextTabTest/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ViewController : UIViewController {
4444
NSFontAttributeName:UIFont(name:"GillSans", size:15)!,
4545
NSParagraphStyleAttributeName:lend {
4646
(p:NSMutableParagraphStyle) in
47-
let terms = NSTextTab.columnTerminators(for:Locale.current())
47+
let terms = NSTextTab.columnTerminators(for:Locale.current)
4848
let tab = NSTextTab(textAlignment:.right, location:170, options:[NSTabColumnTerminatorsAttributeName:terms])
4949
var which : Int { return 2 }
5050
switch which {

bk2ch11p552webkit/ch24p825webview/WebViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessage
171171
act.centerYAnchor.constraint(equalTo:wv.centerYAnchor)
172172
])
173173
// webkit uses KVO
174-
wv.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
174+
wv.addObserver(self, forKeyPath: #keyPath(WKWebView.loading), options: .new, context: nil)
175175
// cool feature, show title
176-
wv.addObserver(self, forKeyPath: "title", options: .new, context: nil)
176+
wv.addObserver(self, forKeyPath: #keyPath(WKWebView.title), options: .new, context: nil)
177177

178178
}
179179
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {
@@ -334,8 +334,8 @@ class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessage
334334
deinit {
335335
print("dealloc")
336336
// using KVO, always tear down, take no chances
337-
self.wv.removeObserver(self, forKeyPath: "loading")
338-
self.wv.removeObserver(self, forKeyPath: "title")
337+
self.wv.removeObserver(self, forKeyPath: #keyPath(WKWebView.loading))
338+
self.wv.removeObserver(self, forKeyPath: #keyPath(WKWebView.title))
339339
// with webkit, probably no need for this, but no harm done
340340
self.wv.stopLoading()
341341
// break all retains

bk2ch11p553webkit2/ch24p825webview/WebViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class WebViewController: UIViewController, UIViewControllerRestoration {
7777
act.centerYAnchor.constraint(equalTo:wv.centerYAnchor)
7878
])
7979
// webkit uses KVO
80-
wv.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
80+
wv.addObserver(self, forKeyPath: #keyPath(WKWebView.loading), options: .new, context: nil)
8181
// cool feature, show title
82-
wv.addObserver(self, forKeyPath: "title", options: .new, context: nil)
82+
wv.addObserver(self, forKeyPath: #keyPath(WKWebView.title), options: .new, context: nil)
8383

8484
wv.navigationDelegate = self
8585

@@ -126,8 +126,8 @@ class WebViewController: UIViewController, UIViewControllerRestoration {
126126
deinit {
127127
print("dealloc")
128128
// using KVO, always tear down, take no chances
129-
self.wv.removeObserver(self, forKeyPath: "loading")
130-
self.wv.removeObserver(self, forKeyPath: "title")
129+
self.wv.removeObserver(self, forKeyPath: #keyPath(WKWebView.loading))
130+
self.wv.removeObserver(self, forKeyPath: #keyPath(WKWebView.title))
131131
// with webkit, probably no need for this, but no harm done
132132
self.wv.stopLoading()
133133
}

bk2ch12p567observingNSProgress/ch25p840customThermometer/MyProgressView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MyProgressView: UIView {
1111
let ins : CGFloat = 2.0
1212
let r = self.bounds.insetBy(dx: ins, dy: ins)
1313
let radius : CGFloat = r.size.height / 2.0
14-
let mpi = .pi
14+
let mpi = CGFloat.pi
1515
let path = CGMutablePath()
1616
path.moveTo(nil, x: r.maxX - radius, y: ins)
1717
path.addArc(nil,

bk2ch12p567observingNSProgress/ch25p840customThermometer/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ViewController: UIViewController {
7676
// architecture 3: explicit KVO on an NSProgress, update progress view manually
7777

7878
self.op3 = ProgressingOperation(units:10)
79-
self.op3!.progress.addObserver(self, forKeyPath: "fractionCompleted", options: [.new], context: nil)
79+
self.op3!.progress.addObserver(self, forKeyPath: #keyPath(Progress.fractionCompleted), options: [.new], context: nil)
8080
self.op3!.start()
8181

8282
}

bk2ch15p660EmbeddedAVKit/EmbeddedAVKit/ViewController.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ViewController: UIViewController {
104104
}
105105
*/
106106

107-
av.addObserver(self, forKeyPath: "readyForDisplay", options: .new, context:nil)
107+
av.addObserver(self, forKeyPath: #keyPath(AVPlayerViewController.isReadyForDisplay), options: .new, context:nil)
108108

109109
return; // just proving you can swap out the player
110110
delay(3) {
@@ -116,11 +116,17 @@ class ViewController: UIViewController {
116116

117117
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?,
118118
context: UnsafeMutablePointer<()>?) {
119-
guard keyPath == "readyForDisplay" else {return}
120-
guard let vc = object as? AVPlayerViewController else {return}
121-
guard let ok = change?[NSKeyValueChangeKey.newKey] as? Bool else {return}
122-
guard ok else {return}
123-
vc.removeObserver(self, forKeyPath:"readyForDisplay")
119+
guard keyPath == #keyPath(AVPlayerViewController.isReadyForDisplay)
120+
else {return}
121+
guard let vc = object as? AVPlayerViewController
122+
else {return}
123+
guard let ok = change?[NSKeyValueChangeKey.newKey] as? Bool
124+
else {return}
125+
guard ok
126+
else {return}
127+
vc.removeObserver(
128+
self,
129+
forKeyPath:#keyPath(AVPlayerViewController.isReadyForDisplay))
124130

125131
DispatchQueue.main.async {
126132
self.finishConstructingInterface(vc)

bk2ch15p661EmbeddedAVKit2/EmbeddedAVKit/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ class ViewController: UIViewController {
8989
self.view.addSubview(av.view)
9090
av.didMove(toParentViewController: self)
9191
av.addObserver(
92-
self, forKeyPath: "readyForDisplay", options: .new, context: nil)
92+
self, forKeyPath: #keyPath(AVPlayerViewController.isReadyForDisplay), options: .new, context: nil)
9393
}
9494

9595
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {
96-
guard keyPath == "readyForDisplay" else {return}
96+
guard keyPath == #keyPath(AVPlayerViewController.isReadyForDisplay) else {return}
9797
guard let vc = object as? AVPlayerViewController else {return}
9898
guard let ok = change?[NSKeyValueChangeKey.newKey] as? Bool else {return}
9999
guard ok else {return}
100-
vc.removeObserver(self, forKeyPath:"readyForDisplay")
100+
vc.removeObserver(self, forKeyPath:#keyPath(AVPlayerViewController.isReadyForDisplay))
101101
DispatchQueue.main.async {
102102
self.finishConstructingInterface(vc)
103103
}

bk2ch15p672AVKitComposition/ch28p939playerLayer/ViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ViewController: UIViewController {
3939
let asset = AVURLAsset(url:m)
4040
let item = AVPlayerItem(asset:asset)
4141
let p = AVPlayer(playerItem:item)
42-
p.addObserver(self, forKeyPath:"status", context:nil)
42+
p.addObserver(self, forKeyPath:#keyPath(AVPlayer.status), context:nil)
4343
let vc = AVPlayerViewController()
4444
vc.player = p
4545
vc.view.frame = CGRect(10,10,300,200)
@@ -50,7 +50,7 @@ class ViewController: UIViewController {
5050
}
5151

5252
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {
53-
if keyPath == "status" {
53+
if keyPath == #keyPath(AVPlayer.status) {
5454
DispatchQueue.main.async {
5555
self.finishConstructingInterface()
5656
}
@@ -63,7 +63,7 @@ class ViewController: UIViewController {
6363
if p.status != .readyToPlay {
6464
return
6565
}
66-
p.removeObserver(self, forKeyPath:"status")
66+
p.removeObserver(self, forKeyPath:#keyPath(AVPlayer.status))
6767
vc.view.isHidden = false
6868

6969
// absolutely no reason why we shouldn't have a synch layer if we want one
@@ -150,7 +150,7 @@ class ViewController: UIViewController {
150150

151151
// note this cool trick! the status won't change, so to trigger a KVO notification,
152152
// ...we supply the .Initial option
153-
p.addObserver(self, forKeyPath:"status", options:.initial, context:nil)
153+
p.addObserver(self, forKeyPath:#keyPath(AVPlayer.status), options:.initial, context:nil)
154154
p.replaceCurrentItem(with: item)
155155
(sender as! UIControl).isEnabled = false
156156
}

0 commit comments

Comments
 (0)