OC_08_01 KVC

来源:互联网 发布:后期用什么软件 编辑:程序博客网 时间:2024/06/04 20:10

在ViewController

#import "ViewController.h"
#import "Student.h"
#import "Book.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     设计模式 -- 观察者模式
     设计模式是用来解决某些特定问题的
     
     
     观察者模式
     什么是观察者模式?
     当一个类需要发送消息给多个类的时候,就要观察者模式
     
     
     什么时候使用观察者模式?
     在工程中,一些类去观察A类,当'A类'发生变化时,这些观察类就会收到消息,做出相应反应.
     
     观察者模式作用?
     一对多发送消息.
     
     如何在OC中实现观察者模式?
     OC中观察者模式的设计基础:'KVC/KVO'
     KVC 的由来,在IOS2.0之前,并没属性来访问类中的实例变量,那时候开发人员使用'键值编码'的方式来访问类的实例变量,即'KVC'方式.
     
     KVC(key-Value Coding)键值编码
     通过指定表示要访问的属性名字字符串的标识符,可以进行类的属性读取和设置.
     KVC提供了一种在运行时而非编译时动态访问对象属性与实例变量的方式.
     */
    
    
    Student *student = [Student new];
    
    //赋值
    [student setValue:@"Yong" forKey:@"_name"];
    [student setValue:@23 forKey:@"_age"];
    [student setValue:@"GZ" forKey:@"_address"];
    
    NSLog(@"%@",[student description]);
    
    //访问
    NSString *studentName = [student valueForKey:@"_name"];
    NSNumber *studentAge = [student valueForKey:@"_age"];
    NSString *studentAddewss = [student valueForKey:@"_address"];
    NSLog(@"studentName = %@ studentAge = %@ studentAddress = %@",studentName,studentAge,studentAddewss);
    
    
    //动态访问某些属性时,使用一些可以在运行时而不是编译时改变的值的例子
    student.p1 = 1;
    student.p2 = 2;
    student.p3 = 3;
    int num = [student getValuePropertyName:@"p1"];
    NSLog(@"num = %d",num);
    
    
    int num1 = [student getValuePropertynewName:@"p2"];
    NSLog(@"num1 = %d",num1);
    
    
    //键路径编码
    Book *book = [Book new];
    
    [student setValue:book forKeyPath:@"_book"];
    [student setValue:@"Harry Potter" forKeyPath:@"_book._bookName"];
    NSLog(@"book = %@",book);
    
    NSString *bookName = [student valueForKeyPath:@"_book._bookName"];
    NSLog(@"bookName = %@",bookName);
    
    [student setValue:@"玩鸡鸡" forKey:@"ttt"];
    NSLog(@"%@",[student valueForKey:@"ttt"]);
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


在Student.h文件里

#import <Foundation/Foundation.h>
#import "Book.h"

@interface Student : NSObject
{
    NSString *_name;
    NSUInteger _age;
    Book *_book;
}

@property(nonatomic,strong)NSString *address;

@property(nonatomic)int p1;
@property(nonatomic)int p2;
@property(nonatomic)int p3;

-(int)getValuePropertyName:(NSString *)pName;

-(int)getValuePropertynewName:(NSString *)pName;

@property(nonatomic,strong)NSString *hobby;

@end


在Student.m文件里

@implementation Student

@synthesize hobby = ttt;

-(NSString *)description
{
    return [NSString stringWithFormat:@"my name is %@ and age is %ld,address = %@",_name,_age,_address];
    
}

-(int)getValuePropertyName:(NSString *)pName
{
    if ([pName isEqualToString:@"p1"])
    {
        return self.p1;
    }
    else if ([pName isEqualToString:@"p2"])
    {
        return self.p2;
    }
    else if([pName isEqualToString:@"p3"])
    {
        return self.p3;
    }
    return 0;
        
}

-(int)getValuePropertynewName:(NSString *)pName
{
    NSNumber *pNumber = [self valueForKey:pName];
    return [pNumber intValue];
    
}

@end


在Book.h文件里

@interface Book : NSObject
{
    NSString *_bookName;
}

@end


在Book.m文件里

-(NSString *)description
{
    return [NSString stringWithFormat:@"bookName is %@",_bookName];
}
@end

0 0
原创粉丝点击