直接上代码
// 可变数组
@property (nonatomic, strong) NSMutableArray *dataArray;
// 监听
_dataArray = [NSMutableArray new];
[self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// 代理
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"%@",change);
}
// 记得移除
-(void)dealloc{
[self removeObserver:self forKeyPath:@"dataArray"];
}
// 添加数据
- (void)addItemObserver{
[[self mutableArrayValueForKey:@"dataArray"] addObject:@"foo"];
}
// 删除数据
- (void)removeItemObserver{
[[self mutableArrayValueForKey:@"dataArray"] removeLastObject];
}
创建一个安全的定时器
https://github.com/xjh093/JHSafeTimer