- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我用 .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 为 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 中,这是一个使用 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/
简短版本: -removeObserver:forKeyPath:有什么用? ? 为什么不总是使用 -removeObserver:forKeyPath:context: ? 长版 在开发 Cocoa
我是另一个初学者,试图了解 Cocoa 及其复杂性。我正在读德沃的《Objective-C》。在键值编码部分中有一些 setValue:forKeyPath: 的示例。不知何故,尽我所能,我无法让它工
我有一个 UIView 子类,它在 NSDictionary 中注册自身和一个带有对象的观察者。 当此 View 被删除时(当其包含的 View 被删除时),我在 View 的 dealloc 方法中
我创建了一个观察器来跟踪AVPlayer的“速率”。每当AVPlayer速率按预期变化时,都会显示观察者通知。但是,当我尝试在AVPlayer正在播放的项目上结束播放时删除观察者时,出现以下崩溃: *
如果我的实例正在观察另一个对象的某些属性,我是否应该在 dealloc 中调用 removeObserver:forKeyPath:? 最佳答案 是 ...除非有更合适的时间提早执行。 观察者被保存为
我一直在努力寻找示例,但我所看到的在我的案例中不起作用。 下面的代码等价于什么: object.addObserver(self, forKeyPath: "keyPath", options: [.
if (mPlayerItem){ [mPlayerItem removeObserver:self forKeyPath:kStatusKey]; [[NSNotificationC
我正在使用以下代码来了解当前播放 时间。但是当我使用 MPMusicPlayerController 播放歌曲时它不起作用。请帮我解决这个问题。任何想法或建议都将不胜感激 [self addObser
NSMutableDicitionary 的setValue 方法中的forKey 和forKeyPath 有什么区别?我在文档中查找了它们,它们对我来说似乎是一样的。我尝试了以下代码,但无法区分其中
我有一些代码大量使用 KVO,并且在多个地方有 addObserver:forKeyPath: 和 removeObserver:forKeyPath:。该应用程序偶尔会崩溃并显示“无法删除关键路径的
我在从 Xcode 的“崩溃”部分检索到以下崩溃日志时遇到了一些问题。只有少数设备受到此崩溃报告的影响。 我已经分析了问题,但我猜这是 Apple 框架上的错误。但我找不到复制它的方法。 这里有一个类
假设我有一些类似于以下内容的 KVC 兼容对象: class Person : NSObject { var office: Office var firstName: String
我遇到了一个小问题: Player *player1; Player *player2; Player *player3; Player *player4; 在 Player 类中,我有一个名为 sc
我正在研究 RestKit 关系映射示例,但无法理解这些方法调用的目的是什么,或者调用中是否存在拼写错误。他们指的是什么?对象加载器何时会在这些关键路径处遇到内容? [objectManager.ma
根据Apple和我见过的大量例子,使用KVO/KVC来观察自己是没有问题的。另外,根据这些相同的来源,通过在对象的 init 方法中使用 addObserver:forKeypath:options:
我用 .Old | 创建了一个观察者.新的 选项。在处理程序方法中,我尝试获取前后值,但编译器提示:'NSString' is not convertible to 'NSDictionaryInde
有一次我在使用 KVO 时忘记调用“removeObserver:forKeyPath:”。在iOS10模拟器上,模拟器崩溃了。但在iOS11模拟器上一切正常。没有内存泄漏,也没有崩溃。 我很困惑为什
我有一个 ViewController 类,它有一个模型属性,我想在模型属性发生变化时观察它。在我的模型对象中,我有一个属性会在我的应用程序后台定期更新。当它更新时,我需要在我的 ViewContro
我是一名优秀的程序员,十分优秀!