mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`. As proposed in SE-0107: UnsafeRawPointer. `void*` imports as `UnsafeMutableRawPointer`. `const void*` imports as `UnsafeRawPointer`. Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer. * Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer. This requires explicit memory binding in several places, particularly in NSData and CoreAudio. * Fix a bunch of test cases for Void->Raw migration. * qsort takes IUO values * Bridge `Unsafe[Mutable]RawPointer as `void [const] *`. * Parse #dsohandle as UnsafeMutableRawPointer * Update a bunch of test cases for Void->Raw migration. * Trivial fix for the SceneKit test case. * Add an UnsafeRawPointer self initializer. This is unfortunately necessary for assignment between types imported from C. * Tiny simplification of the initializer.
48 lines
1.0 KiB
Swift
48 lines
1.0 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: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
|
if context == &observeContext {
|
|
if let change_ = change {
|
|
if let amount = change_[.newKey] 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)
|
|
|
|
|