Skip to content

Commit e024f8e

Browse files
committed
Works with Defaults 🚀
1 parent fdb021d commit e024f8e

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

‎TableViewDemoUsingCocoaBindings/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@
749749
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
750750
</textFieldCell>
751751
<connections>
752-
<binding destination="zv4-DV-QQ2" name="value" keyPath="objectValue" id="mwC-ak-CmW"/>
752+
<binding destination="zv4-DV-QQ2" name="value" keyPath="objectValue.name" id="Jfb-70-znp"/>
753753
<outlet property="delegate" destination="XfG-lQ-9wD" id="xMb-LV-JIt"/>
754754
</connections>
755755
</textField>
@@ -769,7 +769,7 @@
769769
</subviews>
770770
</clipView>
771771
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="CM4-l6-7mG">
772-
<rect key="frame" x="1" y="119" width="223" height="15"/>
772+
<rect key="frame" x="1" y="-15" width="0.0" height="16"/>
773773
<autoresizingMask key="autoresizingMask"/>
774774
</scroller>
775775
<scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="NO" id="qOj-cp-zpd">

‎TableViewDemoUsingCocoaBindings/ViewController.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import Cocoa
22
import Defaults
33

4+
class Dream : NSObject {
5+
@objc dynamic var name : String
6+
init(name : String) {
7+
self.name = name
8+
}
9+
}
10+
411
extension Defaults.Keys {
512
static let dreams = Defaults.Key<Array<String>>("dreams", default: [
613
"Hit the gym",
@@ -12,13 +19,15 @@ extension Defaults.Keys {
1219
}
1320

1421
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
15-
var dreams = defaults[.dreams]
22+
var dreamNames = defaults[.dreams]
23+
var dreams = [Dream]()
1624
@objc dynamic var selectedIndexes = IndexSet()
1725

1826
@IBOutlet weak var table: NSTableView!
1927

2028
override func viewDidLoad() {
2129
super.viewDidLoad()
30+
dreams = dreamNames.map{Dream(name: $0)}
2231
}
2332

2433
override var acceptsFirstResponder : Bool {
@@ -41,7 +50,7 @@ class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelega
4150

4251
func addNewDream() {
4352
let last = dreams.count
44-
dreams.append("")
53+
dreams.append(Dream(name: ""))
4554
table.insertRows(at: IndexSet(integer: last), withAnimation: .effectGap)
4655
table.scrollRowToVisible(last)
4756
table.selectRowIndexes([last], byExtendingSelection: false)
@@ -61,10 +70,10 @@ class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelega
6170
saveDreams()
6271
}
6372

64-
override func controlTextDidChange(_ obj: Notification) {
73+
override func controlTextDidEndEditing(_ obj: Notification) {
6574
saveDreams()
6675
}
67-
76+
6877
@IBAction func addTableRow(_ sender: Any) {
6978
addNewDream()
7079
}
@@ -74,7 +83,7 @@ class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelega
7483
}
7584

7685
func saveDreams() {
77-
defaults[.dreams] = dreams
86+
defaults[.dreams] = dreams.map({ $0.name })
7887
}
7988
}
8089

0 commit comments

Comments
 (0)