-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sebastian Boldt
committed
Mar 16, 2018
1 parent
2a9831b
commit e3afea1
Showing
5 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Behavioral.playground/Pages/Observer.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
protocol Observer : class { | ||
func willChange(propertyName: String, newPropertyValue: Any?) | ||
func didChange(propertyName: String, oldPropertyValue: Any?) | ||
} | ||
|
||
final class Observable { | ||
|
||
weak var observer: Observer? | ||
private let observablePropertyName = "observableProperty" | ||
|
||
var observableProperty: Int = 0 { | ||
willSet(newValue) { | ||
observer?.willChange(propertyName: observablePropertyName, newPropertyValue: newValue) | ||
} | ||
didSet { | ||
observer?.didChange(propertyName: observablePropertyName, oldPropertyValue: oldValue) | ||
} | ||
} | ||
} | ||
|
||
final class Spy : Observer { | ||
func willChange(propertyName: String, newPropertyValue: Any?) { | ||
print("will change to value \(newPropertyValue)") | ||
} | ||
|
||
func didChange(propertyName: String, oldPropertyValue: Any?) { | ||
print("changed from oldValue \(oldPropertyValue)") | ||
} | ||
} | ||
|
||
let observable = Observable() | ||
let observer = Spy() | ||
observable.observer = observer | ||
|
||
observable.observableProperty = 200 | ||
|
||
//: [Next](@next) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
Binary file modified
BIN
+5.06 KB
(160%)
GOFSwift.xcworkspace/xcuserdata/sebastianboldt.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters