iOS 【KVC的拓展-key&keyPath区别/修改私有属性/取值/数组求和&平均值】

2021年10月23日 13点热度 0条评论 来源: 王中尧
//
//  main.m
//  KVC
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h"
#import "Bone.h"
#import "Book.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Person *p = [[Person alloc] init];
        
        Book *book1 = [[Book alloc] init];
        book1.name = @"5分钟突破iOS开发";
        book1.price = 10.5;
        
        Book *book2 = [[Book alloc] init];
        book2.name = @"5分钟突破android开发";
        book2.price = 18.5;
        
        Book *book3 = [[Book alloc] init];
        book3.name = @"5分钟突破前端开发";
        book3.price = 20.5;
        
        Book *book4 = [[Book alloc] init];
        book4.name = @"5分钟突破PHP开发";
        book4.price = 10.5;
        
        p.books = @[book1, book2, book3, book4];
        
        // 获得所有的书名(将所有的书名放到一个数组中)
//        NSMutableArray *names = [NSMutableArray array];
//        for (Book *book in p.books) {
//            [names addObject:book.name];
//        }
        
        // 取出books数组中每一个元素的name属性值,放到一个新的数组中返回
//        NSArray *names = [p valueForKeyPath:@"books.name"];
//        [p valueForKeyPath:@"dog.name"];
//        NSLog(@"%@", names);
        
        // 返回对象某个属性的平均值、总值(注意返回的是 对象,要用NSNumber对象接受,数字包装成对象肯定是NSNumber)
//        NSNumber *avgNumber = [p valueForKeyPath:@"books.@avg.price"];
        NSNumber *sumNumber = [p valueForKeyPath:@"books.@sum.price"];
        NSLog(@"%@", sumNumber);
//        NSLog(@"%f", [sumNumber doubleValue]);
    }
    return 0;
}

/**
 *  取值
 */
void test3()
{
    Person *p = [[Person alloc] init];
    p.dog = [[Dog alloc] init];
    p.dog.bone = [[Bone alloc] init];
    
    //        Cocoa的大招
    
    NSLog(@"%@", [p valueForKeyPath:@"dog.bone"]);
}

/**
 *  key和keyPath的区别
 */
void test2()
{
    Person *p = [[Person alloc] init];
    p.dog = [[Dog alloc] init];
    p.dog.bone = [[Bone alloc] init];
    //        p.dog.bone.type = @"狗骨";
    
    //        [p setValue:@"猪骨" forKeyPath:@"dog.bone.type"];
    //        [p.dog setValue:@"猪骨" forKeyPath:@"bone.type"];
    [p.dog.bone setValue:@"猪骨" forKeyPath:@"type"];
    
    NSLog(@"%@", p.dog.bone.type);
    
    //        p.dog.name = @"wangwang";
    //        [p.dog setValue:@"wangcai" forKey:@"name"];
    //        [p.dog setValue:@"larry" forKeyPath:@"name"];
    
    //        [p setValue:@"hashiqi" forKeyPath:@"dog.name"];
    
    //forKeyPath包含了forKey的功能,以后使用forKeyPath就可以了
    //forKeyPath中可以利用.运算符, 就可以一层一层往下查找对象的属性。而forKey是无法识别.运算符的,他会讲dog.name看成是一个属性名来对待(.也会错误的看成属性名)。所以以后更多的我们要用forKeyPath
    //        [p setValue:@"hashiqi" forKey:@"dog.name"]; // 写法错误
    
    //        NSLog(@"%@", p.dog.name);
}

void test()
{
    Person *p = [[Person alloc] init];
    //        p.name = @"rose";
    //        p.age = 20;
    //        [p setValue:@"jack" forKey:@"name"];
    //        [p setValue:@30 forKey:@"age"];
    
    // 利用KVC可以随意修改一个对象的属性或者成员变量(并且私有的也可以修改)
    
    //        [p setValue:@"jack" forKeyPath:@"_name"];
    //        [p setValue:@30 forKeyPath:@"_age"];
    //        NSLog(@"%d %@", p.age, p.name);
    
    
    [p setValue:@10.5 forKey:@"height"];
    [p printHeight];
}

    原文作者:王中尧
    原文地址: https://blog.csdn.net/Felicity294250051/article/details/50667383
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。