iOS进阶_KVC(进阶使用)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wtdask/article/details/84977240

GitHub参考Demo

苹果官方文档参考


1、KVC与字典

//KVC与字典
-(void)dictionaryTest{
    WTPerson * p = [WTPerson new];
    NSDictionary * dict = @{
                            @"name":@"wt",
                            @"age":@18,
                            @"nick":@"Cat",
                            @"height":@180,
                            @"dd":@"dd"
                            };
    [p setValuesForKeysWithDictionary:dict];
    NSLog(@"p.name = %@,p.age = %d,p.nick = %@,p.height = %f",p.name,p.age,p.nick,p.height);
    
    NSArray * keys = @[@"name",@"age"];
    NSDictionary * dict1 =[p dictionaryWithValuesForKeys:keys];
    NSLog(@"%@",dict1);
}

2、KVC的消息传递

//KVC的消息传递
-(void) arrayKVCTest{

    //相当于给数组中的所有成员都发送了一遍消息
    NSArray *arr =@[@"Monday",@"Tuesday",@"Wednesday"];
    NSArray *lengthArr = [arr valueForKey:@"length"];
    NSLog(@"%@",lengthArr);
    
    NSArray *lowercaseStringArr = [arr valueForKey:@"lowercaseString"];
    NSLog(@"%@",lowercaseStringArr);
}

3、KVC容器操作

聚合操作符 @avg、@conunt、@max、@min、@sum

    NSMutableArray * students = [NSMutableArray array];
    for (int i=0; i< 6; i++) {
        WTPerson * p = [WTPerson new];
        NSDictionary *dict = @{
                               @"name":@"wt",
                               @"age":@(18+i),
                               @"nick":@"Cat",
                               @"height":@(1.65+0.02*arc4random_uniform(6))
                               };
        [p setValuesForKeysWithDictionary:dict];
        [students addObject:p];
    }
    NSLog(@"所有身高:%@",[students valueForKey:@"height"]);
    
    //平均身高
    float avg = [[students valueForKeyPath:@"@avg.height"] floatValue];
    NSLog(@"平均身高:%f",avg);

数组操作符 @distinctUnionOfObjects @unionOfObjects

NSArray * arr = [students valueForKeyPath:@"@distinctUnionOfObjects.height"];
    NSLog(@"去重操作:%@",arr);
    
    NSArray * arr1 = [students valueForKeyPath:@"@unionOfObjects.height"];
    NSLog(@"不去重操作:%@",arr1);

嵌套集合(array&set)操作 @distinctUnionOfArrays @unionOfArrays @distinctUnionOfSets

NSMutableArray * students1 = [NSMutableArray array];
    for (int i=0; i< 6; i++) {
        WTPerson * p = [WTPerson new];
        NSDictionary *dict = @{
                               @"name":@"wt",
                               @"age":@(18+i),
                               @"nick":@"Cat",
                               @"height":@(1.65+0.02*arc4random_uniform(6))
                               };
        [p setValuesForKeysWithDictionary:dict];
        [students1 addObject:p];
    }

    NSArray * nestArr = @[students,students1];
    NSArray * arr2 = [nestArr valueForKeyPath:@"@distinctUnionOfArrays.height"];
    NSLog(@"去重操作:%@",arr2);
    
    NSArray * arr3 = [nestArr valueForKeyPath:@"@unionOfArrays.height"];
    NSLog(@"不去重操作:%@",arr3);
    
    //无序集合
    //set特点:会自动去重
    NSMutableSet * studentsSet = [NSMutableSet set];
    for (int i=0; i< 6; i++) {
        WTPerson * p = [WTPerson new];
        NSDictionary *dict = @{
                               @"name":@"wt",
                               @"age":@(18+i),
                               @"nick":@"Cat",
                               @"height":@(1.65+0.02*arc4random_uniform(6))
                               };
        [p setValuesForKeysWithDictionary:dict];
        [studentsSet addObject:p];
    }
    NSLog(@"studentsSet = %@",[studentsSet valueForKey:@"height"]);
    
    NSMutableSet * studentsSet1 = [NSMutableSet set];
    for (int i=0; i< 6; i++) {
        WTPerson * p = [WTPerson new];
        NSDictionary *dict = @{
                               @"name":@"wt",
                               @"age":@(18+i),
                               @"nick":@"Cat",
                               @"height":@(1.65+0.02*arc4random_uniform(6))
                               };
        [p setValuesForKeysWithDictionary:dict];
        [studentsSet1 addObject:p];
    }
    NSLog(@"studentsSet1 = %@",[studentsSet1 valueForKey:@"height"]);
    
    NSSet * nestSet = [NSSet setWithObjects:studentsSet,studentsSet1,nil];
    
    NSArray * arrSet2 = [nestSet valueForKeyPath:@"@distinctUnionOfSets.height"];
    NSLog(@"arrSet2 = %@",arrSet2);

4、KVC集合代理对象

WTPerson.h

#import <Foundation/Foundation.h>

@interface WTPerson : NSObject

/** count  **/
@property(nonatomic,assign)NSUInteger count;

/** penArr  **/
@property(nonatomic,strong)NSMutableArray * penArr;

@end

WTPerson.m

#import "WTPerson.h"

@implementation WTPerson

//集合代理对象:一对多的关系
//array对应下面两个方法
-(NSUInteger) countOfBooks {
    return self.count;
}

-(id) objectInBooksAtIndex:(NSUInteger)index{
    return [NSString stringWithFormat:@"book %lu",index];
}


//set对应下面三个方法
//个数
-(NSUInteger) countOfPens{
    return [self.penArr count];
}

//是否包含这个成语对象
- (id) memberOfPens:(id)object{
    return [self.penArr containsObject:object] ? object : nil;
}

//迭代器
- (id) enumeratorOfPens {
    return [self.penArr objectEnumerator];
}

//- (id) enumeratorOfPens {
//    return [self.penArr reverseObjectEnumerator];
//}

@end

验证

ViewController.m

//集合代理对象
- (void) setTest{
    WTPerson *p =[WTPerson new];
    p.count = 5;
    
    NSLog(@"books = %@",[p valueForKey:@"books"]);
    
    p.penArr = [NSMutableArray arrayWithObjects:@"pen0",@"pen1",@"pen2",@"pen3",nil];
    NSSet * set = [p valueForKey:@"pens"];
    NSLog(@"pens = %@",set);
    
    NSEnumerator * enumerator = [set objectEnumerator];
    NSString * str = nil;
    while (str = [enumerator nextObject]) {
        NSLog(@"%@",str);
    }
}

输出

在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页