mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
includes a number of QoI things to help people write the correct code. I will commit the testcase for it as the next patch. The bulk of this patch is moving the stdlib, testsuite and validation testsuite to the new syntax. I moved a few uses of "as" patterns back to as? expressions in the stdlib as well. Swift SVN r27959
47 lines
1.0 KiB
Swift
47 lines
1.0 KiB
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
|
|
// 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 observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
|
|
if context == &observeContext {
|
|
if let change_ = change {
|
|
if let amount = change_[NSKeyValueChangeNewKey as NSString] as? Int {
|
|
println("Observed value updated to \(amount)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let value = ObservedValue()
|
|
value.amount = 42
|
|
let observer = ValueObserver(value: value)
|
|
// CHECK: updated to 43
|
|
value.amount++
|
|
// CHECK: amount: 43
|
|
dump(value)
|
|
|
|
|