KVC(一)

面试题:

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

image


友情链接:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS KVC(一)基本了解iOS KVC (二) 不可不知的赋值深层次原理iOS KVC (三)不可不知的取值...
    奔跑吧小蚂蚁阅读 1,498评论 1 8
  • KVC KVC定义 KVC(Key-value coding)键值编码,就是指iOS的开发中,可以允许开发者通过K...
    暮年古稀ZC阅读 2,181评论 2 9
  • KVC(Key-value coding)键值编码,单看这个名字可能不太好理解。其实翻译一下就很简单了,就是指iO...
    朽木自雕也阅读 1,591评论 6 1
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 一、是什么 KVC(Key-value coding)键值编码,通过一个Key来访问一个对象的属性 常用方法 Fo...
    快乐的tomato阅读 278评论 0 1