Skip to content

Commit 5ab567a

Browse files
committed
memory management improvements
1 parent b68a6a4 commit 5ab567a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

bk2ch08p410ListOfSwitches/ListOfSwitches/ViewController.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class MyContentView : UIView, UIContentView {
3838
self.addSubview(sw)
3939
sw.center = CGPoint(x:self.bounds.midX, y:self.bounds.midY)
4040
sw.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
41-
sw.addAction(UIAction {[unowned sw] action in
42-
(configuration as? Config)?.delegate?.switchChangedTo(sw.isOn, sender:self)
41+
sw.addAction(UIAction { action in
42+
if let sender = action.sender as? UISwitch {
43+
(configuration as? Config)?.delegate?.switchChangedTo(sender.isOn, sender:sender)
44+
}
4345
}, for: .valueChanged)
4446
config()
4547
}

bk2ch08p410ListOfSwitches2/ListOfSwitches/ViewController.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ class MyContentView : UIView, UIContentView {
4141
sw.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
4242
sw.topAnchor.constraint(equalTo: self.topAnchor, constant:10).isActive = true
4343
sw.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant:-10).isActive = true
44-
sw.addAction(UIAction {[unowned sw] action in
45-
(configuration as? Config)?.isOnChanged?(sw.isOn, self)
44+
sw.addAction(UIAction { action in
45+
if let sender = action.sender as? UISwitch {
46+
(configuration as? Config)?.isOnChanged?(sender.isOn, sender)
47+
}
4648
}, for: .valueChanged)
4749
config()
4850
}
@@ -91,7 +93,7 @@ class ViewController2: UIViewController {
9193
var datasource : UITableViewDiffableDataSource<String,UniBool>!
9294
override func viewDidLoad() {
9395
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
94-
self.datasource = UITableViewDiffableDataSource<String,UniBool>(tableView: self.tableView) { tv, ip, isOn in
96+
self.datasource = UITableViewDiffableDataSource<String,UniBool>(tableView: self.tableView) { [unowned self] tv, ip, isOn in
9597
let cell = tv.dequeueReusableCell(withIdentifier: "cell", for: ip)
9698
var config = Config()
9799
config.isOn = isOn.bool
@@ -117,4 +119,7 @@ class ViewController2: UIViewController {
117119
snap.appendItems((1...100).map {_ in UniBool(uuid: UUID(), bool: false)})
118120
self.datasource.apply(snap, animatingDifferences: false)
119121
}
122+
deinit {
123+
print("farewell2") // check memory management
124+
}
120125
}

0 commit comments

Comments
 (0)