Objective-C语言——KVC设计模式


ViewController.m


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

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
     
     设计模式--观察者模式
     设计模式是用来解决某些特定问题
     观察者模式
     什么是观察者模式
     当一个类需要发送消息给多个类的时候,就用观察者模式
     
     什么时候是有观察者模式呢?
     在工程中,一下类取观察A类,当‘A类’发生变化时,这些观察类就会收到消息,作出相应反应
     
     观察者模式的作用?
     一对多的发送消息;
     
     如何在OC中实现观察者模式呢?
     OC中观察者模式的设计基础“KVC/KVO”
     
     KVC的由来在ios2.0之前,并没用属性来访问类中的实例变量,那时候开发人员是有‘键值编码’的方式
     来访问类的实例变量,即‘KVC’的方式
     
     什么是KVC (keh- Value Coding)键值编码
     通过指定表示要访问的属性名字的字符串标识符,可以进行类的属性读取和设置
     KVC提供了一种在运行时而非编译是动态访问对象属性与实例变量的方式。
     
     */
    Student *student=[Student new];
    
    //赋值
    [student setValue:@"Rick" forKey:@"_name"];
    [student setValue:@25 forKey:@"_age"];
    [student setValue:@"GZ" forKey:@"_address"];
    NSLog(@"%@",[student description]);
    
    //访问
    NSString *studentName=[student valueForKey:@"_name"];//去掉下划线也能运行的
    NSInteger studentAge=[student valueForKey:@"_age"];
    NSString *studentAddress=[student valueForKey:@"_address"];
    NSLog(@"studentName=%@ studentAge=%@ studentAddress=%@",studentName,studentAge,studentAddress);
    
    
    //动态访问某些属性时,是有一些可以在运行时而不是编译时改变的值 的例子
    student.p1=1;
    student.p2=2;
    student.p3=3;
    int num=[student getValueProoertyName:@"p1"];
    NSLog(@"num=%d",num);
    
    int num1=[student getValueProoertyNewName:@"p2"];
    NSLog(@"num1=%d",num1);
    
    //键路径编码
    Book *book=[Book new];
    
    //Book是student的成员变量
    [student setValue:book forKey:@"_book"];//forKeyPath可以不用forKey
    //book对象赋给student的成员变量
    [student setValue:@"Harry Potter" forKeyPath:@"_book._bookName"];//访问student里面访问Book的成员变量
    NSLog(@"bookName=%@",[book description]);
    
    
    NSString *bookName=[student valueForKeyPath:@"_book._bookName"];
    NSLog(@"bookName=%@",bookName);
    
    //[student setValue:@"play the guiter" forKey:@"_hobby"];
    //NSLog(@"%@",[student valueForKey:@"_hoddy"]);//字典差不多意思是用key的名访问value
    
    
    
    [student setValue:@"play the guiter" forKey:@"ttt"];
    NSLog(@"%@",[student valueForKey:@"ttt"]);//字典差不多意思是用key的名访问value
    
    

    
}

- (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;
    NSInteger _age;
    Book *_book;

}


@property(nonatomic,strong)NSString *address;

@property(nonatomic)int p1;

@property(nonatomic)int p2;

@property(nonatomic)int p3;

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

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

@property(nonatomic,strong)NSString *hobby;
@end


Student.m

#import "Student.h"

@implementation Student

@synthesize hobby=ttt;

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

-(int)getValueProoertyName:(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)getValueProoertyNewName:(NSString *)pName
{
    NSNumber *pNumber=[self valueForKey:pName];
    return [pNumber intValue];
}

@end


Book.h

#import <Foundation/Foundation.h>

@interface Book : NSObject
{
    NSString *_bookName;
}

@end



Book.m

#import "Book.h"

@implementation Book

-(NSString *)description//他是在API他是类方法但现在用实例方法说明他是属性
{
    return [NSString stringWithFormat:@"bookName is %@",_bookName];
}

@end



运行结果

2015-12-20 17:53:42.738 OC_08_01[2583:68217] my name is Rick and age is 25 ,address =GZ

2015-12-20 17:53:42.739 OC_08_01[2583:68217] studentName=Rick studentAge=25 studentAddress=GZ

2015-12-20 17:53:42.740 OC_08_01[2583:68217] num=1

2015-12-20 17:53:42.740 OC_08_01[2583:68217] num1=2

2015-12-20 17:53:42.740 OC_08_01[2583:68217] bookName=bookName is Harry Potter

2015-12-20 17:53:42.741 OC_08_01[2583:68217] bookName=Harry Potter

2015-12-20 17:53:42.741 OC_08_01[2583:68217] play the guiter


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值