美文网首页
KVC(1) - 进阶用法1

KVC(1) - 进阶用法1

作者: JinHuiZhang | 来源:发表于2019-07-24 20:55 被阅读0次

    首先创建一个Person类,在.h文件中定义属性

    #import <Foundation/Foundation.h>
    
    @interface TZPerson : NSObject
    
    @property (nonatomic, strong) NSString* name;
    @property (nonatomic, assign) int age
    @property (nonatomic, strong) NSString* nick;
    @property (nonatomic, assign) float height;
    
    @end
    

    在ViewController中作如下测试

    KVC字典操作

    /// KVC字典操作
    - (void) dictionaryTest {
        TZPerson* p = [TZPerson new];
        
        NSDictionary* dict = @{
                               @"name":@"Tom",
                               @"age":@18,
                               @"nick":@"Cat",
                               @"height":@180,
                               @"dd":@"helo"
                               };
        
        [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);
    }
    

    自定Key值要和Person属性对应,否者就会出现异常,下面是解决异常办法,在Person类中实现:

     //赋值key值不存在
    - (void) setValue:(id)value forUndefinedKey:(NSString *)key {
        NSLog(@"key = %@值不存在 ", key);
    }
    

    KVC消息传递 array

    - (void) arrayKVCTest {
        NSArray* arr = @[@"Monday", @"Tuesday", @"Wednesday"];
    
    //length、lowercaseString、uppercaseString...都是NSString的成员变量
        NSArray* lengthArr = [arr valueForKey:@"length"];
        NSLog(@"%@", lengthArr);
        
        NSArray* lowercaseArr = [arr valueForKey:@"lowercaseString"];
        NSLog(@"%@", lowercaseArr);
    }
    

    聚合操作符

    /// 聚合操作符  @avg、@count、@max、@min、@sum
    - (void) contrainerTest {
        
        NSMutableArray* students = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students addObject:student];
        }
        
        NSLog(@"%@", [students valueForKey:@"height"]);
        
        /// 平均身高    @聚合操作符.属性
        float avg = [[students valueForKeyPath:@"@avg.height"] floatValue];
        NSLog(@"%f", avg);
    
    }
    

    数组操作符

    @distinctUnionOfObjects : 去重
    @unionOfObjects : 不去重

    /// 数组操作符 @distinctUnionOfObjects @unionOfObjects
    - (void) contrainerArrayTest {
        
        NSMutableArray* students = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students addObject:student];
        }
        
        NSLog(@"%@", [students valueForKey:@"height"]);
        
        
        NSArray* arr = [students valueForKeyPath:@"@distinctUnionOfObjects.height"];
        NSLog(@"arr = %@", arr);
        
        NSArray* arr1 = [students valueForKeyPath:@"@unionOfObjects.height"];
        NSLog(@"arr1 = %@", arr1);
    }
    

    嵌套操作符

    @distinctUnionOfArrays
    @unionOfArrays
    @distinctUnionOfSets

    /// 嵌套集合(array&set)操作 @distinctUnionOfArrays @unionOfArrays @distinctUnionOfSets
    - (void) containerNestingTest {
        
        NSMutableArray* students = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students addObject:student];
        }
        
        NSMutableArray* students1 = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students1 addObject:student];
        }
        
        // 嵌套数组
        NSArray* nestArr = @[students, students1];
        
        
        NSArray* arr = [nestArr valueForKeyPath:@"@distinctUnionOfArrays.height"];
        NSLog(@"arr = %@", arr);
        
        NSArray* arr1 = [nestArr valueForKeyPath:@"@unionOfArrays.height"];
        NSLog(@"arr1 = %@", arr1);
        
    }
    
    - (void) containerNestingTest1 {
        
        NSMutableSet* students = [NSMutableSet set];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students addObject:student];
        }
        
        NSLog(@"students = %@", [students valueForKey:@"height"]);
        
        NSMutableSet* students1 = [NSMutableSet set];
        for (int i = 0; i < 6; i++) {
            TZPerson* student = [TZPerson new];
            NSDictionary* dict = @{
                                   @"name":@"Tom",
                                   @"age":@(18+i),
                                   @"nick":@"Cat",
                                   @"height":@(1.65 + 0.02*arc4random_uniform(6)),
                                   };
            [student setValuesForKeysWithDictionary:dict];
            [students1 addObject:student];
        }
        
         NSLog(@"students1 = %@", [students1 valueForKey:@"height"]);
        
        NSSet* nestSet = [NSSet setWithObjects:students, students1, nil];
        
        NSArray* arr1 = [nestSet valueForKeyPath:@"@distinctUnionOfSets.height"];
        NSLog(@"arr1 = %@", arr1);
    }
    

    集合代理对象 (重要)

    
    

    相关文章

      网友评论

          本文标题:KVC(1) - 进阶用法1

          本文链接:https://www.haomeiwen.com/subject/wkvfrctx.html