Open zwaldowski opened 7 years ago
@swift-ci create
Ah, this is the same as the older KVO API: you need to pass `options: .new` to have the change dictionary fully initialized. This should be documented alongside the final API. Shall I close this or leave it open as a documentation request?
Comment by Andrew McLean (JIRA)
I don't see documentation for passing options with the new new NSObject.observer(keyPath: AnyKeyPath) -> NSKeyValueObservation api? Can you pass the options parameter as well?
Comment by Martin Massera (JIRA)
When I pass options: [.new, .old] , this works well with Int or String, but does not work with enum. Did not try with other types.
This does not work:
import Foundation
@objc enum Enum: Int {
case A = 0
case B
}
class Foo: NSObject {
@objc dynamic var bar = Enum.A
}
let foo = Foo()
let observer = foo.observe(\.bar, options: [.new, .old]) { o, change in
print("old: \(change.oldValue) - new: \(change.newValue)")
}
foo.bar = Enum.B
However, this does work:
import Foundation
class Foo: NSObject {
@objc dynamic var bar = 0
}
let foo = Foo()
let observer = foo.observe(\.bar, options: [.new, .old]) { o, change in
print("old: \(change.oldValue) - new: \(change.newValue)")
}
foo.bar = 1
Environment
Xcode Version 9.0 beta (9M136h) / swiftlang 900.0.43Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 5 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 25ddd18650c37c688c03b65c1a59f909Issue Description:
Consider the following:
NSKeyValueObservedChange
doesn't appear to be initialized correctly. This is not just the print; value getters from the change object also don't work.