面试题:
1.通过KVC修改属性会触发KVO?
析:会
2.KVC的赋值和取值的过程?以及原理?
KVO:Key-Vaule coding “键值编码” 可以通过一个key来访问属性
常见的api
- (void)setValue:(nullable id)value forKey:(NSString *)key;
- (nullable id)valueForKey:(NSString *)key;
- (nullable id)valueForKeyPath:(NSString *)keyPath;
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;
...
触发KVO以及setValue方法
#import "ViewController2.h"
@interface Animal: NSObject
@property (nonatomic,assign)CGFloat height;
@end
@implementation Animal
@end
@interface Person:NSObject{
@public
Animal *_aimal;
}
@property (nonatomic,strong)NSString *name;
@end
@implementation Person{
NSString *_address;
Animal *_aimal2;
}
-(instancetype)init{
if(self = [super init]){
self->_aimal2 = [[Animal alloc]init];
}
return self;
}
-(void)setName:(NSString *)name{
_name = name;
}
@end
@interface ViewController2 ()
@property (nonatomic,strong)Person *p;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.p = [Person new];
self.p->_aimal = [[Animal alloc]init];
[self.p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
[self.p addObserver:self forKeyPath:@"_address" options:NSKeyValueObservingOptionNew context:nil];
[self.p addObserver:self forKeyPath:@"_aimal.height" options:NSKeyValueObservingOptionNew context:nil];
[self.p addObserver:self forKeyPath:@"_aimal2.height" options:NSKeyValueObservingOptionNew context:nil];
}
//使用kvc来赋值 触发了KVO
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.p setValue:@"Lucy" forKey:@"name"];
[self.p setValue:@"Beijing" forKey:@"_address"];
[self.p setValue:@(1.888) forKeyPath:@"_aimal.height"];
[self.p setValue:@(2.999) forKeyPath:@"_aimal2.height"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"KVO监听到了属性%@的变化了%@",keyPath,change);
}
-(void)dealloc{
[self.p removeObserver:self forKeyPath:@"name"];
[self.p removeObserver:self forKeyPath:@"_address"];
[self.p removeObserver:self forKeyPath:@"_aimal.height"];
[self.p removeObserver:self forKeyPath:@"_aimal2.height"];
NSLog(@"%s",__FUNCTION__);
}
@end
我们先来看看为什么会触发KVO:
#import "ViewController4.h"
@interface Person_:NSObject
@property (nonatomic,strong)NSString *name;
@end
@implementation Person_
-(void)setName:(NSString *)name{
_name = name;
}//打个断点2
@end
@interface ViewController4 ()
@property (nonatomic,strong)Person_ *p;
@end
@implementation ViewController4
- (void)viewDidLoad {
[super viewDidLoad];
self.p = [Person_ new];
[self.p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
}
//使用kvc来赋值 触发了KVO
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.p setValue:@"Lucy" forKey:@"name"];//打个断点1
}
//打个断点3
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"KVO监听到了属性%@的变化了%@",keyPath,change);
}
-(void)dealloc{
[self.p removeObserver:self forKeyPath:@"name"];
NSLog(@"%s",__FUNCTION__);
}
@end
分析:在3处打断点
然后点击下一步(Step into)进入汇编,发现
image
image
image
友情链接:
网友评论