mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
48 lines
1.1 KiB
Swift
48 lines
1.1 KiB
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
// REQUIRES: executable_test
|
|
|
|
// REQUIRES: objc_interop
|
|
|
|
// rdar://problem/19060227
|
|
|
|
import Foundation
|
|
|
|
class ObservedValue: NSObject {
|
|
dynamic var amount = 0
|
|
}
|
|
|
|
class ValueObserver: NSObject {
|
|
private var observeContext = 0
|
|
let observedValue: ObservedValue
|
|
|
|
init(value: ObservedValue) {
|
|
observedValue = value
|
|
super.init()
|
|
observedValue.addObserver(self, forKeyPath: "amount", options: .new, context: &observeContext)
|
|
}
|
|
|
|
deinit {
|
|
observedValue.removeObserver(self, forKeyPath: "amount")
|
|
}
|
|
|
|
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
|
|
if context == &observeContext {
|
|
if let change_ = change {
|
|
if let amount = change_[NSKeyValueChangeNewKey as String] as? Int {
|
|
print("Observed value updated to \(amount)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let value = ObservedValue()
|
|
value.amount = 42
|
|
let observer = ValueObserver(value: value)
|
|
// CHECK: updated to 43
|
|
value.amount += 1
|
|
// CHECK: amount: 43
|
|
dump(value)
|
|
|
|
|