OC KVC总结

  在iOS开发中,我们一般使用set方法或者点语法来修改对象的属性值,比如说 stu.age = 9 与 [stu setAge:9]。

  KVC(key value coding)键值编码,这是一种间接修改对象属性值的方法。实现方法就是通过用字符串来描述要修改的属性。基本的操作方法有 setValue:forKey: 和 valueForKey,以字符串的形式发送对象

  特别提醒,使用KVC中所有的value都必须是对象。

在此以Student类和 Book类作为例子来总结

  Student类:

 //.h文件
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject @property (nonatomic, assign) int age; @property (nonatomic, copy) NSString* name; @property (nonatomic, strong) Book* book; @property (nonatomic, strong) NSArray* books;
@end //.m文件 #import "Student.h"
#import "Book.h"
@implementation Student - (NSString *) description
{
return [NSString stringWithFormat:@"%@ %d", _name, _age];
}
@end

  Book类:

  

 //.h文件
#import <Foundation/Foundation.h> @interface Book : NSObject @property (nonatomic, assign) int price;
@end //.m文件
#import "Book.h" @implementation Book @end

  主函数:

  

 #import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h" //1.设值 取值
void fun1()
{
NSLog(@"==============fun1==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSLog(@"%@", stu); NSLog(@"name : %@", [stu valueForKey:@"name"]);
} //2.批量获取属性值
void fun2()
{
NSLog(@"==============fun2==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSArray *arr = @[@"name", @"age"]; NSDictionary *dict = [stu dictionaryWithValuesForKeys:arr]; NSLog(@"%@", dict);
} //3.批量设置属性值(通过字典批量赋值)
void fun3()
{
NSLog(@"==============fun3==============");
Student *stu = [[Student alloc] init]; NSDictionary *dict = @{@"age": @, @"name" : @"悟空"}; [stu setValuesForKeysWithDictionary:dict]; NSLog(@"%@", stu);
} //4.通过键路径(key path)访问(使用valueForKeyPath)
void fun4()
{
NSLog(@"==============fun4==============");
Student *stu = [[Student alloc] init];
Book *book = [[Book alloc] init];
stu.book = book;
//设置值
[stu setValue:@ forKeyPath:@"book.price"];
//获取值
NSNumber *price = [stu valueForKeyPath:@"book.price"]; NSLog(@"price: %@", price);
} //5.对数组进行整体操作
void fun5()
{
NSLog(@"==============fun5==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
// NSArray *prices = [stu valueForKeyPath:@"books.price"];
NSArray *prices = [stu.books valueForKeyPath:@"price"];
NSLog(@"prices: %@", prices);
} //6.键路径的运算符(数组元素求和)
void fun6()
{
NSLog(@"==============fun6==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@sum.price"];
NSLog(@"prices: %@", sum);
} //7.键路径的运算符(数组总数count)
void fun7()
{
NSLog(@"==============fun7==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@count.price"];
NSLog(@"num: %@", sum);
} //8.键路径的运算符(获取数组不同元素)
void fun8()
{
NSLog(@"==============fun8==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSArray *array = [stu valueForKeyPath:@"books.@distinctUnionOfObjects.price"];
NSLog(@"array: %@", array);
} //9.求数组元素中得最大值、最小值、平均值
void fun9()
{
NSLog(@"==============fun9==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *max = [stu valueForKeyPath:@"books.@max.price"];
NSNumber *min = [stu valueForKeyPath:@"books.@min.price"];
NSNumber *average = [stu valueForKeyPath:@"books.@avg.price"];
NSLog(@"max: %@", max);
NSLog(@"min: %@", min);
NSLog(@"avg: %@", average);
} int main(int argc, const char * argv[])
{ @autoreleasepool {
fun1();
fun2();
fun3();
fun4();
fun5();
fun6();
fun7();
fun8();
fun9();
}
return ;
}

  

上一篇:top命令参数解析


下一篇:Java NIO 读取文件、写入文件、读取写入混合