iOS keyValue方法的使用

不暗的黑夜發表於2020-10-26
//TZPerson是普通的繼承自NSObject的類
TZPerson.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

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

@end

//在外部view中使用keyValue 來呼叫TZPerson的屬性
ViewController.m
/// KVC字典操作
- (void) dictionaryTest {
    TZPerson* p = [TZPerson new];

    NSDictionary* dict = @{
                           @"name":@"Tom",
                           @"age":@18,
                           @"nick":@"Cat",
                           @"height":@180,
                           @"dd":@"helo"
                           };
    //根據字典裡的key給P物件賦對應的value值
    [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);

}

/// KVC訊息傳遞  array
- (void) arrayKVCTest {
    /*對陣列而言,valueForKey方法的作用是把valueForKey的引數以訊息的方式傳送給陣列的每一個元素*/
    NSArray* arr = @[@"Monday", @"Tuesday", @"Wednesday"];
    //獲取arr陣列中每一個元素的length屬性並返回一個陣列
    NSArray* lengthArr = [arr valueForKey:@"length"];
    NSLog(@"%@", lengthArr);
    
    //arr陣列中每一個元素執行lowercaseString方法並結果返回給lowercaseArr陣列
    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"]);
    /// 平均身高, @avg是獲取平均值的聚合運算子,其他的以此類推
    float avg = [[students valueForKeyPath:@"@avg.height"] floatValue];
    NSLog(@"%f", avg);

}
/// 陣列操作符 @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"]);
    
    //distinctUnionOfObjects陣列中去重
    NSArray* arr = [students valueForKeyPath:@"@distinctUnionOfObjects.height"];
    NSLog(@"arr = %@", arr);
    //unionOfObjects不去重
    NSArray* arr1 = [students valueForKeyPath:@"@unionOfObjects.height"];
    NSLog(@"arr1 = %@", arr1);
}

 

相關文章