天天看点

KVC的使用(对一个对象的成员变量进行操作(赋值/取值))

切记:请求的数据要存在相应的类中,不能在加载试图中请求数据

KVC就是对请求数据的一个简化

MainViewController.m

#import "MainViewController.h"
#import "Student.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    //KVC的使用
    
    Student *stu = [[Student alloc] init];
    
    //KVC的作用:对一个对象的成员变量进行操作(赋值/取值)
    
    //赋值的方法
    [stu setValue:@"adfasdf" forKey:@"name"];
//    [stu setValue:@"米4像苹果1" forKey:@"Name"];  //第一个查找
//     [stu setValue:@"米4像苹果2" forKey:@"_Name"];   //找不到
//    [stu setValue:@"米4像苹果3" forKey:@"_name"];  //
   
    
    //这个是一个类中的属性赋值
//    stu setValue:<#(id)#> forKeyPath:<#(NSString *)#>
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"222", @"StudentID", @"liuyafang", @"name" ,nil];
    
    [stu setValuesForKeysWithDictionary:dic];
    
    
    //取值的方法
    NSLog(@"%@",[stu valueForKey:@"name"]);
    
    
    //KVO  键值观察  (注册一个观察者)(自己监视自己对象的内容)
    
    //参数1:观察谁
    //参数2:观察哪个属性
    //参数3:在实现方法中获得新值合适旧值
    //参数4:任意的指针类型
    [stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"aaa"];
    
    stu.name = @"pingguo45";
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@",keyPath);
    NSLog(@"%@",object);
    NSLog(@"%@",change);
    NSLog(@"%@",context);
}
- (void)dealloc
{
//    self removeObserver:<#(NSObject *)#> forKeyPath:<#(NSString *)#>
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end      

Student.h

#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic , retain)NSString *name;
@property (nonatomic , retain)NSString *sex;
@property (nonatomic , retain)NSString *studengID;
//利用kvc对model进行封装
- (instancetype) initWihtDictionary:(NSDictionary *)dic;
@end      
#import "Student.h"
@implementation Student
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    //这个方法在类的内部实现,就用就是纠错
    //一旦在赋值过程中,发现key没有周到对应的成员变量,就会调用这个发放
    //如果没有重写个方法,就会crash
    
    if ([key isEqualToString:@"id"]) {
        _studengID = value;
    }
}
- (id)valueForUndefinedKey:(NSString *)key
{
    //取值的纠错方法
    if ([key isEqualToString:@"id"]) {
        return  self.superclass;
    }
    
    return nil;
}
- (instancetype) initWihtDictionary:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
- (void)dealloc
{
    [_name release];
    [_sex release];
    [super dealloc];
}
@end      

继续阅读