美文网首页
IOS键值编码KVC

IOS键值编码KVC

作者: 张威_9cfe | 来源:发表于2020-04-07 00:13 被阅读0次

    KVC机制允许以字符串形式间接操作对象的属性

    FKItem.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface FKItem : NSObject
    
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, assign) int price;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    FKItem.m

    #import "FKItem.h"
    
    @implementation FKItem
    {
        NSDate *date;
    }
    
    //处理非法设置不存在的key,在类的实现部分重写setValue: forUndefinedKey:方法,避免闪退
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
        NSLog(@"你设置的key:[%@] 不存在!", key);
    }
    
    //处理非法访问不存在的key,在类的实现部分重写setValue: forUndefinedKey:方法,避免闪退
    - (id)valueForUndefinedKey:(NSString *)key
    {
        NSLog(@"你访问的key:[%@] 不存在!", key);
        return nil;
    }
    
    //给基本类型赋值为nil会报异常,在类的实现部分重写setNilValueForKey
    - (void)setNilValueForKey:(NSString *)key
    {
        if ([@"price" isEqualToString:key])
        {
            _price = 0;
        }
        else
        {
            //回调父类的setNilValueForKey,执行默认行为
            [super setNilValueForKey:key];
        }
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"name:%@ price:%d date:%@", _name, _price, date];
    }
    @end
    

    FKOrder.h

    #import <Foundation/Foundation.h>
    #import "FKItem.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface FKOrder : NSObject
    
    @property (nonatomic, strong) FKItem *item;
    @property (nonatomic, assign) int amount;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    FKOrder.m

    #import "FKOrder.h"
    
    @implementation FKOrder
    
    @end
    

    main.m

    #import <Foundation/Foundation.h>
    #import "FKItem.h"
    #import "FKOrder.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            FKItem *item = [[FKItem alloc] init];
            [item setValue:@"苹果" forKey:@"name"];
            [item setValue:[NSNumber numberWithInt:5] forKey:@"price"];
            [item setValue:[[NSDate alloc] init] forKey:@"date"];//可以给类实现部分定义的date成员赋值
            NSLog(@"name:%@ price:%@ date:%@", [item valueForKey:@"name"], [item valueForKey:@"price"], [item valueForKey:@"date"]);
            
            //访问不存在的key,会报异常
            [item setValue:@"233" forKey:@"namm"];
            [item valueForKey:@"namm"];
            //给基本类型赋值为nil,会报异常
            [item setValue:nil forKey:@"price"];
            NSLog(@"name:%@ price:%@ date:%@", [item valueForKey:@"name"], [item valueForKey:@"price"], [item valueForKey:@"date"]);
            
            FKOrder *order = [[FKOrder alloc] init];
            [order setValue:@"12" forKey:@"amount"];
            [order setValue:item forKey:@"item"];
            NSLog(@"%@ %@", [order valueForKey:@"amount"], [order valueForKey:@"item"]);
            
            //访问复合属性,用forKeyPath
            [order setValue:@"键盘" forKeyPath:@"item.name"];
            [order setValue:@"100" forKeyPath:@"item.price"];
            NSLog(@"%@ %@", [order valueForKeyPath:@"item.name"], [order valueForKeyPath:@"item.price"]);
        }
        return 0;
    }
    

    输出

    2020-04-07 00:09:02.306795+0800 KVCTest[15122:475463] name:苹果 price:5 date:Tue Apr  7 00:09:02 2020
    2020-04-07 00:09:02.306999+0800 KVCTest[15122:475463] 你设置的key:[namm] 不存在!
    2020-04-07 00:09:02.307140+0800 KVCTest[15122:475463] 你访问的key:[namm] 不存在!
    2020-04-07 00:09:02.307237+0800 KVCTest[15122:475463] name:苹果 price:0 date:Tue Apr  7 00:09:02 2020
    2020-04-07 00:09:02.309560+0800 KVCTest[15122:475463] 12 name:苹果 price:0 date:2020-04-06 16:09:02 +0000
    2020-04-07 00:09:02.309722+0800 KVCTest[15122:475463] 键盘 100
    

    相关文章

      网友评论

          本文标题:IOS键值编码KVC

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