不务正业之OC(六)–KVC介绍

KVC可以在不定义对象属性访问器、定义成private时,也可以直接访问。
缺点:KVC错误发现,一般在运行期

定义两个类Person.h、House.h,这个看头文件就行,不需要实现

//  Person.h
 
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
    @private
    NSString *_name;
    float _salary;
}
 
@end
 
//  House.h
 
#import <Foundation/Foundation.h>
#import "Person.h"
 
@interface House : NSObject
{
@private
    Person *_person;
    NSMutableArray *_famliy;
    NSMutableArray *_money;
}
 
@end

main.m

//  main.m
 
#import <Foundation/Foundation.h>
#import "House.h"
//#import "Person.h"  在House.h已导入
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        Person *person = [[Person alloc] init];
        //KV访问
        [person setValue:@"lijie" forKey:@"_name"];
        NSString *name1 = [person valueForKey:@"_name"];
        NSLog(@"name1 is: %@",name1);
        NSString *name2 = [person valueForKey:@"name"];
        NSLog(@"name2 is: %@",name2);
 
        //路径访问
        House *house = [[House alloc] init];
        [house setValue:person forKeyPath:@"_person"];
        [house setValue:@"liyi" forKeyPath:@"_person._name"];
        NSString *myson = [house valueForKeyPath:@"_person._name"];
        NSLog(@"house owner is: %@",myson);
 
        //一对多
        NSMutableArray *famliy = [NSMutableArray arrayWithCapacity:30];
        Person *person1 = [[Person alloc] init];
        NSString *name_a = [NSString stringWithFormat:@"monther"];
        [person1 setValue:name_a forKey:@"_name"];
        Person *person2 = [[Person alloc] init];
        NSString *name_b = [NSString stringWithFormat:@"grandma"];
        [person2 setValue:name_b forKey:@"_name"];
        [famliy addObject:person1];
        [famliy addObject:person2];
 
        [house setValue:famliy forKey:@"_famliy"];
        NSArray *whos1 = [house valueForKeyPath:@"_famliy._name"];
        NSLog(@"%@",whos1);
 
        NSArray *whos2 = [famliy valueForKey:@"_name"];
        NSLog(@"%@",whos2);
 
        //简单运算 sum、min、max、avg、count
        [person1 setValue:[NSNumber numberWithFloat:2.1] forKey:@"_salary"];
        [person2 setValue:[NSNumber numberWithFloat:7.1] forKey:@"_salary"];
        NSMutableArray *money = [NSMutableArray arrayWithCapacity:3];
        [money addObject:person1];
        [money addObject:person2];
        [house setValue:money forKey:@"_money"];
 
 
        NSNumber *sum = [house valueForKeyPath:@"_money.@sum._salary"];
        NSLog(@"sum is %@",sum);
 
        NSNumber *min = [house valueForKeyPath:@"_money.@min._salary"];
        NSLog(@"min is %@",min);
 
        NSNumber *max = [house valueForKeyPath:@"_money.@max._salary"];
        NSLog(@"max is %@",max);
 
        NSNumber *avg = [house valueForKeyPath:@"_money.@avg._salary"];
        NSLog(@"avg is %@",avg);
 
        NSNumber *count = [house valueForKeyPath:@"_money.@count"];
        NSLog(@"count is %@",count);   
    }
    return 0;
}
KVC[13423:303] name1 is: lijie
KVC[13423:303] name2 is: lijie
KVC[13423:303] house owner is: liyi
KVC[13423:303] (
    monther,
    grandma
)
KVC[13423:303] (
    monther,
    grandma
)
KVC[13423:303] sum is 9.2
KVC[13423:303] min is 2.1
KVC[13423:303] max is 7.1
KVC[13423:303] avg is 4.6
KVC[13423:303] count is 2

Post a Comment