无需烦恼的 Objective-C 块 KVO 库,可自动移除观察者
目前市面上有众多 KVO 库。没有找到同时支持块和自动移除观察者的库。这通常就是我需要的。只是想绑定一些东西。
使用方法很简单。我建议将 <NSObject+CLBlockObservation.h>
包含在您的 .pch
文件中。
[observee addObserver:observer keyPath:@"p1" block:^(id oldValue, id newValue) {
NSLog(@"do something great!");
}];
还支持观察多个 keypaths
[observee addObserver:observer keyPaths:@[ @"p1", @"p2" ] block:^{
NSLog(@"be awesome!");
}];
## Tests
There's an XCTest project in this repository. It contains a few tests that verify
the lifetime management of the observation and a good sample reference. To run it:
1. Clone this repository
2. Run `pod install` from the `./TestProject` directory
3. Run tests from XCode (Command + U).
## Author
Elad Ben-Israel, [email protected]
## License
CLBlockObservation is available under the MIT license. See the LICENSE file for more info.