Files
swift-mirror/test/Interpreter/SDK/Reflection_KVO.swift
Chris Lattner 31c01eab73 Change the meaning of "if let x = foo()" back to Xcode 6.4 semantics. The compiler
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
2015-04-30 04:38:13 +00:00

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)