gpt4 book ai didi

swift - KVO : How to get old/new values in observeValue(forKeyPath:. ..) 在 Swift 中?

转载 作者:搜寻专家 更新时间:2023-10-30 21:53:28 27 4
gpt4 key购买 nike

我用 .Old | 创建了一个观察者.新的 选项。在处理程序方法中,我尝试获取前后值,但编译器提示:'NSString' is not convertible to 'NSDictionaryIndex: NSObject, AnyObject

override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<Void>) {

let approvedOld = change[NSKeyValueChangeOldKey] as Bool
let approvedNew = change[NSKeyValueChangeNewKey] as Bool

最佳答案

iOS 11 和 Swift >4.1

iOS 11 和 Swift 4 为 KVO 带来了重大变化。

  • 类应该采用@objcMembers注解,以启用KVO或KVO静默失败。
  • 要观察的变量必须声明为dynamic

这是较新的实现,

@objcMembers
class Approval: NSObject {

dynamic var approved: Bool = false

let ApprovalObservingContext = UnsafeMutableRawPointer(bitPattern: 1)

override init() {
super.init()

addObserver(self,
forKeyPath: #keyPath(approved),
options: [.new, .old],
context: ApprovalObservingContext)
}

override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
guard let observingContext = context,
observingContext == ApprovalObservingContext else {
super.observeValue(forKeyPath: keyPath,
of: object,
change: change,
context: context)
return
}

guard let change = change else {
return
}

if let oldValue = change[.oldKey] {
print("Old value \(oldValue)")
}

if let newValue = change[.newKey] {
print("New value \(newValue)")
}

}

deinit {
removeObserver(self, forKeyPath: #keyPath(approved))
}
}

KVO 也有新的基于 bock 的 api,它是这样工作的,

@objcMembers
class Approval: NSObject {

dynamic var approved: Bool = false

var approvalObserver: NSKeyValueObservation!

override init() {
super.init()
approvalObserver = observe(\.approved, options: [.new, .old]) { _, change in
if let newValue = change.newValue {
print("New value is \(newValue)")
}

if let oldValue = change.oldValue {
print("Old value is \(oldValue)")
}
}

}
}

基于 block 的 api 看起来 super 好用。此外,KeyValueObservation 在 deinited 时失效,因此没有删除观察者的硬性要求。

Swift 2.0 和 iOS < 10

在 Swift 2.0 中,这是一个使用 KVO 的类的完整实现,

 class Approval: NSObject {

dynamic var approved: Bool = false

let ApprovalObservingContext = UnsafeMutablePointer<Int>(bitPattern: 1)

override init() {
super.init()
addObserver(self, forKeyPath: "approved", options: [.Old, .New], context: ApprovalObservingContext)
}

override func observeValueForKeyPath(keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer<Void>) {

if let theChange = change as? [String: Bool] {

if let approvedOld = theChange[NSKeyValueChangeOldKey] {
print("Old value \(approvedOld)")
}

if let approvedNew = theChange[NSKeyValueChangeNewKey]{
print("New value \(approvedNew)")

}

return
}
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}

deinit {
removeObserver(self, forKeyPath: "approved")
}
}

let a = Approval()
a.approved = true

关于swift - KVO : How to get old/new values in observeValue(forKeyPath:. ..) 在 Swift 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com