由于Object-C中NSMutableArray
等可变集合不能kvo,所以使用ReactiveObjC
实现观察NSMutableArray
等可变集合内容变化,得到KVO效果,用来触发数据源.
NSMutableArray
@weakify(self)
[RACObserve(self, array) ax_subscribeNext:^(id _Nonnull x) {
@strongify(self)
doSoming;
}];
NSMutableArray *array = NSMutableArray.array;
[array ax_valueChangeObserve:^(NSMutableArray * _Nonnull array) {
NSLog(@"array = %@",array);
}];
/// 添加元素
NSString *str = [NSString stringWithFormat:@"array-%ld",array.count];
[array addObject:str];
NSString *str = [NSString stringWithFormat:@"array-%ld",array.count];
[array insertObject:str atIndex:0];
if (array.count>0) {
[array removeLastObject];
}
NSMutableDictionary
NSMutableDictionary *dict = NSMutableDictionary.dictionary;
[dict ax_valueChangeObserve:^(NSMutableDictionary * _Nonnull dict) {
NSLog(@"dict = %@",dict);
}];
NSString *key = [NSString stringWithFormat:@"key-%ld",dict.count];
dict[key] = key;
AXCollectionObserve is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AXCollectionObserve'
或者
pod 'AXCollectionObserve',:git => 'https://gitee.com/axinger/AXCollectionObserve.git'
axinger, [email protected]
AXCollectionObserve is available under the MIT license. See the LICENSE file for more info.