OC中KVC设计模式简单实用<代码演示>
2014-03-08 23:40
405 查看
#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);
//作者对象类
@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);
相关文章推荐
- C++设计模式:简单工厂类模式<初探>
- OC中数组和可变数组用法<代码演示>
- OC中字符串使用NSString<代码演示>
- Cocos2d-x简单Box2d代码示例|点击碰撞>AppDelegate.h及AppDelegate.cpp<代码演示>
- OC中字典NSDictionary和可变字典NSMutableDictionary<代码演示>
- IOS-CoreData简单使用的主要代码<代码演示>
- Cocos2d-x简单Box2d代码示例|点击碰撞>HelloWorldScene.h<代码演示>
- IOS简单地图效果<代码演示>
- Cocos2d-x简单Box2d代码示例|点击碰撞>HelloWorldScene.cpp<代码演示>
- cocos2d-x简单动画:处理Texture2D和plist文件处理动画<代码演示>
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第一部分:开始场景<后续会提供源码下载链接>
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第九部分:游戏场景GameScene.h<后续会提供源码下载链接>
- 所有的问题, 包括自己想象的问题 <<总结项目用到的设计模式>>
- <<.NET B/S 架构实践>> 几种概念区别 - 算法、设计模式、企业应用架构模式、架构模式
- 自我学习成长系列之<<FirstHead设计模式>>
- <设计模式4>建造者模式
- <转载> 设计模式精解c++-GoF 23 种设计模式解析
- 请用代码简单描述一下Singleton、抽象工厂、Bridge、Composite(任选三个)的设计模式的概念
- JAVA设计模式(11) —<结构型>组合模式(Composite)
- <Head First 设计模式>:装饰者模式--Beverage