kvocontroller

MVVM的KVO属性绑定自定义

て烟熏妆下的殇ゞ 提交于 2021-01-01 03:04:29
我们在使用MVVM设计模式的时候会需要监听数据模型的属性变化,使用RAC是比较简洁好用的,但是如果不想引入RAC这样重量级的框架,那我们该如何编写符合监听需求的框架呢,我们的思路是封装系统的KVO,封装添加和删除监听的代码,改变的值通过block返回。 1、首先我们定义一个单例KVOController来统一管理属性监听,每个监听都有个回调。 typedef void(^ObserverKeyPathDidChange)(id value); @interface KVOController : NSObject + (instancetype)shareInstance; - (void)mj_observeObject:(NSObject *)object forKeyPath:(NSString *)keyPath observerKeyPathDidChange:(ObserverKeyPathDidChange)observerKeyPathDidChange; @end #import "KVOController.h" #import <objc/runtime.h> #import "MJObserver.h" #import "NSObject+OBExtention.h" @interface KVOController () @property

How to observe torchLevel in swift?

本秂侑毒 提交于 2019-12-08 03:23:53
问题 How do I observe torchLevel? I have done the following from objective c solutions, no success: private var torchLevel = 0 let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) device.addObserver(self, forKeyPath: "torchLevel", options: .New, context: &torchLevel) override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if context == &torchLevel { let device = object as!