OC中KVC设计模式简单实用<代码演示>

#import  <Foundation/Foundation.h>
// 作者对象类
@interface  Author :  NSObject
{
    
@private
    
NSString  *_authorName;
}

@end

// 书籍对象类
#import  "Author.h"
//KVC--key-value-coding 的使用
@interface  BookKVC :  NSObject
{

@private
    
// 通常变量设置为私有后,在没有访问器和设置的情况下可以使用 KVC
    
// 变量为 public  时可以使用对象指针访问 Book->_bookName
    
NSString  *_booKName; // 书籍名称
    
NSInteger  _price; // 书的价格
    
Author  *_author; // 作者对象
    
NSArray  *_relativeBooks; // 相关书籍

}

@end

     //kvc 模式下可以带 “_author” 也直接是 “author”
    
BookKVC  *book = [[ BookKVC   alloc init ];
    
//public 情况下
//    book->_booKName = @"public containt";
//    [book setValue:@"this is KVC layoutModel" forKey:@"booKName"];
//    NSString *name = [book valueForKey:@"booKName"];
    
    
/* 键值访问 */
    [book 
setValue : @"this is KVC layoutModel"   forKey : @"_booKName" ];
    
NSString  *name = [book  valueForKey : @"_booKName" ];
    
NSLog ( @"%@" ,name);
    
// 通过路径查找
    
Author  *author = [[ Author   alloc init ];
    [author 
setValue : @"this is authorKVC"   forKey : @"_authorName" ];
    
    
/* 路径访问 */
    
// 设置书籍中的作者对象
    [book 
setValue :author  forKey : @"_author" ];
    
// 通过路径获取
    
NSString  *authorName = [book  valueForKeyPath : @"_author._authorName" ];
    
NSLog ( @"%@" ,authorName);
    
// 也可以通过路径访问重新设置其属性
    [book 
setValue : @"this is reSetName by path"   forKeyPath : @"_author.authorName" ];
    
NSString  *newName = [book  valueForKeyPath : @"author.authorName" ];
    
NSLog ( @"%@" ,newName);
    
    
/* 一对多访问 */
    
NSMutableArray  *relateBook = [ NSMutableArray   arrayWithCapacity : 3 ];
    
for  ( int  i =  0  ; i <  3 ; i++) {
        
BookKVC  *oneMore = [[ BookKVC   alloc init ];
        
NSString  *names = [ NSString   stringWithFormat : @"hong%d" ,i];
        
NSInteger  price = i+ 5 ; // 书的价格
        [oneMore 
setValue :names  forKey : @"_booKName" ]; // 设置书名
        [oneMore 
setValue :[ NSNumber   numberWithInt :price]  forKey : @"_price" ]; // 设置书的价格
        [relateBook 
addObject :oneMore];
        
RELEASE_SAFETY (oneMore);
    }
    
// 设置书籍对象的 _relativeBooks
    [book 
setValue :relateBook  forKey : @"_relativeBooks" ];
    
// 获取存入的数组对象
    
NSArray  *getNames = [book  valueForKeyPath : @"_relativeBooks._booKName" ];
    
NSLog ( @"%@" ,getNames);
    
    
    
    
/*KVC 的一些加点运算语法  @sum @min @max @avg @count */
    
// 数组中数的数量
    
NSString  *count = [book  valueForKeyPath : @"_relativeBooks.@count" ];
    
NSLog ( @"%@" ,count);
    
// 数组中书本的价格最大值
    
NSString  *max = [book  valueForKeyPath : @"_relativeBooks.@max._price" ];
    
NSLog ( @"%@" ,max);
    
// 数组书籍的价格的最小值
    
NSString  *min = [book  valueForKeyPath : @"_relativeBooks.@min._price" ];
    
NSLog ( @"%@" ,min);
    
// 数组中书籍价格的平均值
    
NSString  *avg = [book  valueForKeyPath : @"_relativeBooks.@avg._price" ];
    
NSLog ( @"%@" ,avg);
    
// 安全释放
    
RELEASE_SAFETY (book);
    
RELEASE_SAFETY (author);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值