Skip to content

Commit 7c2cdf5

Browse files
committed
new/revised chapter 11 exx
1 parent ed5bab7 commit 7c2cdf5

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

bk1ch11p462notifications3/NotificationRegistrationAlternatives/ViewController.swift

+45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import UIKit
3+
import MediaPlayer
34
import Combine
45

56
class ViewController: UIViewController {
@@ -9,6 +10,7 @@ class ViewController: UIViewController {
910

1011
var observers = Set<NSObject>()
1112
var storage = Set<AnyCancellable>()
13+
var pipeline : AnyCancellable?
1214
var task : Task<(), Never>?
1315
override func viewDidLoad() {
1416
super.viewDidLoad()
@@ -37,6 +39,47 @@ class ViewController: UIViewController {
3739
default: break
3840
}
3941
}
42+
43+
func test() { // just showing syntax; these are not proper tests, that needs doing
44+
do {
45+
let ob = NotificationCenter.default.addObserver(
46+
forName: .MPMusicPlayerControllerNowPlayingItemDidChange,
47+
object: nil, queue: nil) { [unowned self] _ in
48+
self.updateNowPlayingItem()
49+
// ... and so on ...
50+
}
51+
self.observers.insert(ob as! NSObject)
52+
// ...
53+
for ob in self.observers {
54+
NotificationCenter.default.removeObserver(ob)
55+
}
56+
}
57+
do {
58+
self.pipeline = NotificationCenter.default.publisher(
59+
for: .MPMusicPlayerControllerNowPlayingItemDidChange)
60+
.sink { [unowned self] _ in
61+
self.updateNowPlayingItem()
62+
// ... and so on ...
63+
}
64+
// ...
65+
self.pipeline?.cancel()
66+
self.pipeline = nil
67+
}
68+
do {
69+
let stream = NotificationCenter.default.notifications(
70+
named: .MPMusicPlayerControllerNowPlayingItemDidChange)
71+
let task = Task {
72+
for await _ in stream {
73+
self.updateNowPlayingItem()
74+
// ... and so on ...
75+
}
76+
}
77+
self.task = task
78+
// ...
79+
self.task?.cancel()
80+
self.task = nil
81+
}
82+
}
4083

4184
@IBAction func unwind(_:UIStoryboardSegue) {}
4285

@@ -54,5 +97,7 @@ class ViewController: UIViewController {
5497
// self.task = nil
5598
}
5699

100+
func updateNowPlayingItem() {}
101+
57102
}
58103

bk1ch11p466Timer/Timer/ViewController.swift

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import UIKit
3+
import Combine
34

45
class ViewController: UIViewController {
56

@@ -26,6 +27,30 @@ class ViewController: UIViewController {
2627
print("fired2")
2728
}
2829

30+
var pipeline : AnyCancellable?
31+
func test() {
32+
// just showing syntax
33+
self.pipeline = Timer.publish(every: 1, on: .main, in: .default)
34+
.autoconnect()
35+
.sink { print("timer fired at", $0) }
36+
// ...
37+
self.pipeline = nil
38+
}
39+
var task : Task<(), Never>?
40+
func test2() {
41+
// ditto
42+
let timerpub = Timer.publish(every: 1, on: .main, in: .default)
43+
.autoconnect()
44+
self.task = Task {
45+
for await value in timerpub.values {
46+
print("timer fired at", value)
47+
}
48+
}
49+
// ...
50+
self.task?.cancel()
51+
self.task = nil
52+
}
53+
2954

3055
}
3156

bk1ch11p472action2/bk1ch11p472action/ViewController.swift

+23
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,28 @@ class ViewController: UIViewController {
4848

4949
@IBAction func unwind(_ sender: UIStoryboardSegue) {}
5050

51+
var myButton : UIButton = UIButton()
52+
func test() {
53+
let action = UIAction { [unowned self] _ in
54+
let alert = UIAlertController(
55+
title: "Howdy!", message: "You tapped me!", preferredStyle: .alert)
56+
alert.addAction(
57+
UIAlertAction(title: "OK", style: .cancel))
58+
self.present(alert, animated: true)
59+
}
60+
self.myButton.addAction(action, for: .touchUpInside)
61+
}
62+
func test2() {
63+
let action = UIAction { action in
64+
if var resp = action.sender as? UIResponder {
65+
while let r = resp.next {
66+
print(r); resp = r
67+
}
68+
}
69+
}
70+
self.myButton.addAction(action, for: .touchUpInside)
71+
72+
}
73+
5174
}
5275

bk1ch11p477kvo/ch13p355kvo/AppDelegate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Observer {
3232

3333
func register(with observed:Observed) {
3434
let opts : NSKeyValueObservingOptions = [.old, .new]
35-
let ob = observed.observe(\.value, options: opts) { obj, change in
35+
let ob = observed.observe(\.value, options:opts) { obj, change in
3636
// obj is the observed object
3737
// change is an NSKeyValueObservedChange
3838
if let oldValue = change.oldValue {
@@ -46,7 +46,7 @@ class Observer {
4646
// yes, we can leak if we don't say unowned self
4747
}
4848
// the observer must live long enough for the function to be executed
49-
obs.insert(ob)
49+
self.obs.insert(ob)
5050
}
5151

5252

bk1ch11p480delayExample/delayExample/ViewController.swift

+13-8
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,23 @@ class TracksViewController : UIViewController {
4242

4343
class ViewController: UITableViewController {
4444
let albums = [String]()
45-
45+
var which = 0
4646
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
47-
// delay(0.1) {
48-
// let t = TracksViewController(
49-
// mediaItemCollection: self.albums[indexPath.row])
50-
// self.navigationController?.pushViewController(t, animated: true)
51-
// }
52-
Task {
53-
await Task.sleep(0.1)
47+
switch which {
48+
case 0:
49+
delay(0.1) {
5450
let t = TracksViewController(
5551
mediaItemCollection: self.albums[indexPath.row])
5652
self.navigationController?.pushViewController(t, animated: true)
53+
}
54+
case 1:
55+
Task {
56+
await Task.sleep(0.1)
57+
let t = TracksViewController(
58+
mediaItemCollection: self.albums[indexPath.row])
59+
self.navigationController?.pushViewController(t, animated: true)
60+
}
61+
default: break
5762
}
5863
}
5964

0 commit comments

Comments
 (0)