PMKVObserver
PMKVObserver 提供了一个围绕键值观察(KVO)的安全的基于块的包装器,包括 Objective-C 和 Swift 的 API。特性包括:
- 线程安全。观察者可以在发送 KVO 通知的线程之外的线程上注册,并且可以在另一个线程上进行取消。甚至可以同时在两个线程上取消。
- 在观察对象 dealloc 时自动取消注册。
- 注意:目前不支持嵌套键路径,应使用手动取消;更多信息请见 此处
- 支持传递给块的观察对象,并在该观察对象 dealloc 时自动取消注册。这使得您可以在不保留它或处理弱引用的情况下调用
self
的方法。 - 对自动 dealloc 的线程安全。这可以保护对象 dealloc 时在另一线程上接收消息。
- 对 Objective-C 和 Swift 的第一级支持,包括 Swift API 中的强类型。
示例
Swift
// Observe an object for as long as the object is alive.
_ = KVObserver(object: user, keyPath: \User.fullName) { object, _, _ in
// `object` has the same type as `user`
print("User's full name changed to \(object.fullName)")
}
// Convenience methods for working with the change dictionary
_ = KVObserver(object: user, keyPath: \User.fullName, options: [.old, .new]) { _, change, _ in
// unfortunately we don't know what the type of fullName is, so change uses Any
let old = change.old as? String
let new = change.new as? String
if old != new {
print("User's full name changed to \(new ?? "nil")")
}
}
// Unregistering can be done from within the block, even in an .initial callback
_ = KVObserver(object: user, keyPath: \User.fullName, options: [.initial]) { object, _, kvo in
guard !object.fullName.isEmpty else { return }
print("User's full name is \(object.fullName)")
kvo.cancel()
}
// Or you can unregister externally
let token = KVObserver(object: user, keyPath: \User.fullName) { object, _, _ in
print("User's full name changed to \(object.fullName)")
}
// ... sometime later ...
token.cancel()
// You can also pass an observing object and KVO will be unregistered when that object deallocates
_ = KVObserver(observer: self, object: user, keyPath: \User.fullName) { observer, object, _, _ in
// `observer` has the same type as `self`
observer.nameLabel.text = object.fullName
}
Objective-C
Objective-C 提供了与 Swift 相同的所有功能,尽管没有观察者/对象的强类型。
// Observe an object for as long as the object is alive.
[PMKVObserver observeObject:self.user keyPath:@"fullName" options:0
block:^(id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nullable change, PMKVObserver * _Nonnull kvo) {
NSLog(@"User's full name changed to %@", [object fullName]);
}];
// Change dictionary is provided, but without the convenience methods.
[PMKVObserver observeObject:self.user keyPath:@"fullName"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
block:^(id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nullable change, PMKVObserver * _Nonnull kvo) {
NSString *old = change[NSKeyValueChangeOldKey];
NSString *new = change[NSKeyValueChangeNewKey];
if (old != new && (new == nil || ![old isEqualToString:new])) {
NSLog(@"User's full name changed to %@", new);
}
}];
// Unregistering and observing object support is also provided (see Swift examples).
要求
作为框架安装至少需要 iOS 8、OS X 10.9、watchOS 2.0 或 tvOS 9.0。
如果您通过将源代码复制到您的项目中进行安装,它应该在 iOS 7 或更高版本上运行(如果您删除 KVObserver.swift,则可能在 iOS 6 上运行),并且 OS X 10.7 或更高版本。请注意,这些版本尚未经过测试。
PMKVObserver 需要 Xcode 9 或更高版本。
安装
安装后,您可以通过将 import PMKVObserver
(Swift)或 @import PMKVObserver;
(Objective-C)添加到您的代码中来实现这一点。
Carthage
使用 Carthage 安装时,请将以下内容添加到您的 Cartfile 中:
github "postmates/PMKVObserver" ~> 4.0
CocoaPods
使用 CocoaPods 安装时,请将以下内容添加到您的 Podfile 中:
pod 'PMKVObserver', '~> 4.0'
手动安装
您还可以通过将框架添加到您的工作区,或通过将 3 个文件 KVObserver.h,KVObserver.m 和可选的 KVObserver.swift 添加到您的项目中手动安装。
许可证
许可证根据以下之一授权
- Apache许可证版本2.0(《Apache许可证版本2.0》(a href="LICENSE-APACHE">LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT 或 https://open-source.org.cn/licenses/MIT),供您选择。
贡献
除非您明确声明,否则您提出的任何贡献,若有意包含在工作中,应当按照上述方式双许可,没有额外的条款或条件。
版本历史
v4.1.3 (2020-04-01)
v4.1.2 (2019-01-24)
- 修复了观察可选值和KVO返回值为
NSNull
时与其他 Swift 编译器版本(除4.2外)的兼容性问题。
v4.1.1 (2018-11-28)
- 为
PMKVObserver
添加了.object
和.keyPath
属性。在 Swift 中,.keyPath
属性被命名为.objcKeyPath
。
v4.1.0 (2018-11-27)
- 更好地处理可观察的可选值。现在我们提供了更强有力的保证,确保在
.old
和.new
属性中不会返回nil
值。
v4.0.0 (2018-09-05)
-
将 Swift 版本设置为 4。
-
解决 Xcode 10 在
KVObserver.Change
对象中涉及Any
值转换的问题(SR-8704)。 -
在
KVObserver.Change
对象的.old
和.new
属性中处理RawRepresentable
类型。注意:可选的
RawRepresentable
类型(例如由\.foo?.bar
等路径产生的类型)仅在 Swift 4.1 及更高版本中受支持。
v3.0.2 (2018-05-22)
- 在构建时对 CocoaPods 的某些警告进行静音。
v3.0.1 (2017-09-13)
- 将 Swift 版本恢复到 Swift 3.2。新的
KeyPath
东西可以从 3.2 版本使用,因此没有理由要求使用 4。PMVKObserver 仍然可以与 Swift 3.1 编译,但必须使用 Swift 3.2 编译时才能使用KeyPath
API。
v3.0.0 (2017-09-12)
- 转换为 Swift 4。
- 添加使用 Swift 4
KeyPath
的新初始化器。 - 将
Change.rawDict
和Change.kind
设置为非可选。
v2.0.2 (2017-07-25)
- 在拆解时切换到不公正锁。在支持该特性的平台上使用 `
os_unfair_lock
`,否则使用不公正互斥锁。
v2.0.1 (2016-09-15)
- 修复 CocoaPods。
v2.0.0 (2016-09-08)
- Swift 3.0 更新。
v1.0.5 (2016-09-08)
- Swift 2.3 更新。
v1.0.4 (2016-03-02)
- 更新 CocoaPods podspec,将 Swift 支持拆分为子规范。
v1.0.3 (2015-01-28)
- 向
PMKVObserver
添加属性cancelled
。
v1.0.2 (2016-01-26)
- 切换为 MIT 或 Apache 2.0 双授权。
v1.0.1 (2015-12-17)
- 停止泄露我们的
pthread_mutex_t
。
v1.0 (2015-12-17)
初次发布。