KVC和KVO的演示

//
//  CZViewController.m
//  09-KVC&KVO
//
//  Created by apple on 11/07/14.
//  Copyright (c) 2014itcast. All rights reserved.
//

#import"CZViewController.h"
#import
"CZPerson.h"
#import
"CZStudent.h"
#import
"CZBook.h"

@interface CZViewController()
@property(nonatomic,strong)CZStudent*student;
@end

@implementationCZViewController
/**
 1> KVC - Key Value Coding
键值(路径)编码
 
 KVC
是一种间接修改/读取对象属性的一种方式
 
 KVC
被称为苹果开发的大招!
 
 KVC
在使用时,须要注意,键值名称在对象属性中必须存在,不然会崩溃!
 
 2> KVO - Key Value Observer
键值观察(观察者模式)
 
通知中心一样也是观察者模式
 
 */

- (
void)viewDidLoad
{
    [
superviewDidLoad];
   
   
// student
   
CZStudent*student = [[CZStudentalloc]init];
   
self.student= student;
   
    student.
name=@"mary";
    student.
age=19;
    student.
no=@"001";
   
   
//观察student的属性变化
   
// 1> self     负责监听的对象        NSNotificationCenter是由通知中心负责
   
// 2> keyPath  要观察的属性键值路径,KVO主要用于对模型属性变化进行观察的一种模式
   
//       通知中心是经过对象POST字符串给通知中心来达到观察目的的
   
// 3>选项"|"(而且)位运算,能够同时支持多个选项
   
// 4>上下文
    [student
addObserver:selfforKeyPath:@"name"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:@"hello"];
   
    student.
name=@"rose";
}

//分类:又被称为:隐式代理,灵活度很高,可是代码的可读性很差,不建议使用!
// delegate ->显式代理
//
//观察监听方法,全部的监听事件发生时,都统一调用监听者的此方法!
// 1> keyPath  观察的键值路径
// 2> object   观察的对象
// 3> change   改变,新、旧数值发送过来
// 4> context  上下文,创建观察时指定的上下文,若是同时监听多个属性,能够经过上下文加以区分
- (
void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
   
NSLog(@"%@ %@ %@ %@", keyPath, object, change, context);
}

- (
void)demoKVC3
{
   
// student
   
CZStudent*student = [[CZStudentalloc]init];
    student.
name=@"mary";
    student.
age=19;
    student.
no=@"001";
   
   
CZBook*book = [[CZBookalloc]init];
    book.
bookName=@"iOS";
    student.
book= book;
   
   
// student1
   
CZStudent*student1 = [[CZStudentalloc]init];
    student1.
name=@"rose";
    student1.
age=29;
    student1.
no=@"002";
   
   
CZBook*book1 = [[CZBookalloc]init];
    book1.
bookName=@"iPhone";
   
    student1.
book= book1;
   
   
NSArray*students =@[student, student1];
   
NSLog(@"%@", students);
   
   
//需求,将两个学生的姓名生成一个数组
   
//    NSMutableArray *arrayM = [NSMutableArray array];
   
//    for (CZStudent *stu in students) {
   
//        [arrayM addObject:stu.name];
   
//    }
   
//    NSLog(@"%@", arrayM);
   
//KVC取值
   
//使用KVC取值的时候,若是当前对象不包含指定的键值
   
//那么KVC会进入对象内部,继续搜索!
   
NSLog(@"%@", [studentsvalueForKeyPath:@"name"]);
   
NSLog(@"%@", [studentsvalueForKeyPath:@"book.bookName"]);
}

- (
void)demoKVC2
{
   
CZStudent*student = [[CZStudentalloc]init];
    student.
name=@"mary";
    student.
age=19;
    student.
no=@"001";
   
   
CZBook*book = [[CZBookalloc]init];
    book.
bookName=@"iOS";
   
    student.
book= book;
   
   
NSLog(@"%@", student);
   
   
//使用KVC来给对象赋值
    [student
setValue:@"rose"forKeyPath:@"name"];
   
//给数值型属性赋值时,一样能够使用字符串
    [student
setValue:@"16"forKeyPath:@"age"];
   
    [
selfdemo:student];
   
   
NSLog(@"%@", student);
}

- (
void)demo:(CZStudent*)student
{
   
//使用KVCBook赋值
//    CZBook *book = student.book;
//    book.bookName = @"iPhone";
    [student
setValue:@"iPhone"forKeyPath:@"book.bookName"];
}

- (
void)kvcDemo1
{
   
CZPerson*p = [[CZPersonalloc]init];
    p.
name=@"mary";
    p.
age=19;
   
   
NSLog(@"%@", p);
   
   
//使用KVC来给对象赋值
    [p
setValue:@"rose"forKeyPath:@"name"];
   
//给数值型属性赋值时,一样能够使用字符串
    [p
setValue:@"16"forKeyPath:@"age"];
   
   
NSLog(@"%@", p);
}

@end
数组

相关文章
相关标签/搜索