KVC

/*KVC

 KVC,:Key-value coding,它是一种使用字符串标识符,间接访问对象属性的机制,它是很多技术的基础。

 以下代码示例中首先定义了两个类:一个PlayItem(当前正在播放) nameprice两个属性

 一个PlayList(播放列表) namenumbercurrentItemitemList四个属性*/

//示例类在main函数中测试

#import <Foundation/Foundation.h>

#import "PlayList.h"

int main(int argc, const char * argv[])

{

    

    @autoreleasepool {

        //测试KVC

        PlayList *pl = [[PlayList alloc] init];

        

        [pl setValue:@"播放列表" forKey:@"name"];

        [pl setValue:[NSNumbernumberWithInt:10] forKey:@"number"];

        NSLog(@"pl.name = %@,pl.number = %d",pl.name,pl.number);

        

        [pl setValue:@"当前正在播放" forKeyPath:@"currentItem.name"];

        NSLog(@"pl.currentItem.name = %@",pl.currentItem.name);

        

        // 设置 一批 key value

        NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:

                             @"200", @"number",

                             @"陈浩", @"name",nil];

        [pl setValuesForKeysWithDictionary:dic];

        NSLog(@"name is %@,number is %d",pl.name,pl.number);

        

        //[pl setValue:@"hello" forKey:@"test"];

        //因为pl中没有test这个Key 所以奔溃

        

        //通过key获取value

        id obj = [pl valueForKey:@"itemList"];

        NSLog(@"%@",obj);

        

        id obj2 = [pl valueForKeyPath:@"itemList.name"];

        NSLog(@"%@",obj2);

        

        //itemList.price的总和

        NSLog(@"itemList price  sum is %@",[pl.itemListvalueForKeyPath:@"@sum.price"]);

        //itemList.price的平均

        NSLog(@"itemList price avg is %@",[pl.itemListvalueForKeyPath:@"@avg.price"]);

        //itemList.price中最大的

        NSLog(@"itemList price  max is %@",[pl.itemListvalueForKeyPath:@"@max.price"]);

        //itemList.price中最小的

        NSLog(@"itemList price min is %@",[pl.itemListvalueForKeyPath:@"@min.price"]);

    }

    return 0;

}

//PlayItem

#import <Foundation/Foundation.h>

@interface PlayItem : NSObject

@property (nonatomic, retain) NSString *name;

@property (nonatomic, assign) float price;

@end

#import "PlayItem.h"

@implementation PlayItem

//如果设置里边不存在的key就会触发

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    //打印方法名

    NSLog(@"function %@ is calling",NSStringFromSelector(_cmd));

}

@end

//PlayList

#import <Foundation/Foundation.h>

#import "PlayItem.h"

@interface PlayList : NSObject

@property (nonatomic, assign) int number;

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) PlayItem *currentItem;

@property (nonatomic, retain) NSMutableArray *itemList;

@end

#import "PlayList.h"

@implementation PlayList

- (id)init

{

    self = [super init];

    if (self) {

        self.currentItem = [[PlayItem alloc] init];

        self.itemList = [NSMutableArray array];

        for (int i = 0; i < 20; i++) {

            PlayItem *p = [[PlayItem alloc] init];

            p.name = [NSString stringWithFormat:@"name_%d",i];

            p.price = 100 + i;

            [self.itemList addObject:p];

        }

    }

    returnself;

}

@end

原文地址:https://www.cnblogs.com/chenhaosuibi/p/3442535.html