IOS KVC使用小结
来源:互联网 发布:淘宝上怎么买高仿的包 编辑:程序博客网 时间:2024/05/16 18:23
Person.h
日志:
#import <Foundation/Foundation.h>#import "Father.h"#import "Book.h"@interface Person : NSObject {@public NSString *_fullName;@private NSString *_name; Father *_father; NSArray *_books;}@end
Father.h
@interface Father : NSObject { @protected NSString *_name;}@end
Book.h
#import <Foundation/Foundation.h>@interface Book : NSObject {@private NSString *_name; float _price;}@end
使用代码:
#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]){ @autoreleasepool { Person *person = [[Person alloc] init]; //直接访问public变量 person->_fullName = @"ALI TOM"; NSLog(@"_fullName :%@",person->_fullName); //KVC方式 [person setValue:@"TOM" forKey:@"_name"]; NSLog(@"_name :%@", [person valueForKey:@"_name"]); Father *father = [[Father alloc] init]; [father setValue:@"JACK" forKey:@"_name"]; [person setValue:father forKey:@"_father"]; //KVC路径访问 NSLog(@"father.name :%@", [person valueForKeyPath:@"_father._name"]); [person setValue:@"JERRY" forKeyPath:@"_father._name"]; NSLog(@"father.name :%@", [person valueForKeyPath:@"_father._name"]); NSMutableArray *bookArray = [NSMutableArray arrayWithCapacity:3]; for (int i=0; i<3; i++) { Book *book = [[Book alloc] init]; NSString *bookName = [NSString stringWithFormat:@"book%d", i]; [book setValue:bookName forKey:@"_name"]; [book setValue:@((i + 1) * 10.2) forKey:@"_price"]; [bookArray addObject:book]; [book release]; } [person setValue:bookArray forKey:@"_books"]; //获取所有的数 NSArray *array = [person valueForKeyPath:@"_books._name"]; NSLog(@"%@", array); //KVC计算 //通过@count获取集合book个数 NSNumber *bookCount = [person valueForKeyPath:@"_books.@count"]; NSLog(@"book count :%@", bookCount); //价格总和 NSNumber *sum = [person valueForKeyPath:@"_books.@sum._price"]; NSLog(@"sum :%@", sum); //价格平均值 NSNumber *avg = [person valueForKeyPath:@"_books.@avg._price"]; NSLog(@"vag :%@", avg); //最低价格 NSNumber *min = [person valueForKeyPath:@"_books.@min._price"]; NSLog(@"min :%@", min); //最高价格 NSNumber *max = [person valueForKeyPath:@"_books.@max._price"]; NSLog(@"max :%@", max); } return 0;}
日志:
2013-03-17 13:19:13.835 PersonKVCDemo[1388:303] _fullName :ALI TOM2013-03-17 13:19:13.837 PersonKVCDemo[1388:303] _name :TOM2013-03-17 13:19:13.838 PersonKVCDemo[1388:303] father.name :JACK2013-03-17 13:19:13.839 PersonKVCDemo[1388:303] father.name :JERRY2013-03-17 13:19:13.840 PersonKVCDemo[1388:303] ( book0, book1, book2)2013-03-17 13:19:13.840 PersonKVCDemo[1388:303] book count :32013-03-17 13:19:13.841 PersonKVCDemo[1388:303] sum :61.22013-03-17 13:19:13.842 PersonKVCDemo[1388:303] vag :20.42013-03-17 13:19:13.843 PersonKVCDemo[1388:303] min :10.22013-03-17 13:19:13.843 PersonKVCDemo[1388:303] max :30.6
- IOS KVC使用小结
- iOS之KVC使用
- ios KVC KVO的使用
- ios KVC机制的使用
- ios--kvc/kvo使用详解
- IOS中KVC使用注意
- IOS KVC的详细使用
- iOS KVC setValuesForKeysWithDictionary的使用
- KVC小结
- ios:KVC的概述与使用
- ios:KVC的概述与使用
- IOS开发之----KVC的使用
- iOS:KVO/KVC 的概述与使用
- ios:KVC的概述与使用
- iOS 中KVO和KVC的使用
- iOS:KVO/KVC 的概述与使用
- ios:KVC的概述与使用
- 【iOS】KVC 和 KVO 的使用场景
- 设计模式原则篇:(1)单一职责原则--Single Responsibility Principle
- inline hook简介
- 关于qt/qte在Ubuntu/Debain下的安装
- [cc150] 1.3
- 大灰狼笔记四-汇编hello,world
- IOS KVC使用小结
- 关于编译器的一个问题
- [cc150] 1.2
- [cc150] 1.1
- uva 10881 - Piotr's Ants
- java runnable 接口启动线程
- LeetCode Flatten Binary Tree to Linked List
- jQuery ui 翻译汇总之对话框(dialog)二
- 记事本