Objective-C KVC

KVC

KVC(Key-value coding)键值编码,就是指iOS的开发中,可以允许开发者通过Key/keyPath名直接访问对象的属性,或者给对象的属性赋值。。这样就可以在运行时动态地访问和修改对象的属性。主要用途获取对象的私有变量和修改对象私有变量。

KVC通过NSObject的分类公开接口(属于foundation下的NSKeyValueCoding.h 文件)

取值

// key -> id 
- (nullable id)valueForKey:(NSString *)key;

// 验证key对应的值是否符合 通过重写 -validate<Key>:error: 方法实现
- (BOOL)validateValue:(inout id _Nullable * _Nonnull)ioValue forKey:(NSString *)inKey error:(out NSError **)outError;

//key -> NSMutableArray
- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;

//key -> NSMutableOrderedSet. NSMutableOrderedSet可变有序set
- (NSMutableOrderedSet *)mutableOrderedSetValueForKey:(NSString *)key;

//key -> NSMutableSet. NSMutableSet可变无序序set
- (NSMutableSet *)mutableSetValueForKey:(NSString *)key;

// keyPath -> id 通过属性路径 A对象中含有B对象 B对象含有C对象 通过 [A valueForKeyPath@"B.C"]获取C的值
- (nullable id)valueForKeyPath:(NSString *)keyPath;

- (BOOL)validateValue:(inout id _Nullable * _Nonnull)ioValue forKeyPath:(NSString *)inKeyPath error:(out NSError **)outError;

// keyPath -> NSMutableArray
- (NSMutableArray *)mutableArrayValueForKeyPath:(NSString *)keyPath;

// keyPath -> NSMutableOrderedSet
- (NSMutableOrderedSet *)mutableOrderedSetValueForKeyPath:(NSString *)keyPath API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

// keyPath -> NSMutableSet
- (NSMutableSet *)mutableSetValueForKeyPath:(NSString *)keyPath;

// 例如 A中没有B 如果调用 [A valueForKey@"B"] 则会回调此方法 可以重写此方法解决
- (nullable id)valueForUndefinedKey:(NSString *)key;

// 一组key -> 以key为键的字典
- (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;

存值

//根据key 设置值
- (void)setValue:(nullable id)value forKey:(NSString *)key;

//根据keyPath 设置值
- (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;

//如果没有对应的key 会调用此方法 
- (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key;

//针对key对应的类型是NSNumber/NSValue [self setValue:nil forKey@"b"]; 如果value为nil 调用此方法 
- (void)setNilValueForKey:(NSString *)key;

// 以字典的键为key 设置值
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues;

直接访问成员变量

//default return YES. 可以通过KVC设置直接访问/修改成员变量。 return NO 不能通过KVC设置直接访问/修改成员变量
+(BOOL)accessInstanceVariablesDirectly;

KVC访问顺序

使用KVC存值和取值顺序是

  • 先找相关setter/getter方法 找到结束
  • 根据+(BOOL)accessInstanceVariablesDirectly;是否可以直接访问成员变量 可以进行下一步
  • 访问成员变量

 相关方法和成员变量访问顺序

//属性名name
@property (nonatomic, copy) NSString *name;

取值 

相关getter方法

-(id)getName;  ->  -(id)name;  ->  -(id)isName;

成员变量

_name -> _isName -> name -> isName

存值

相关setter方法

-(void)setName; -> -(void)setIsName;

成员变量

_name -> _isName -> name -> isName

 

集合操作

@interface A : NSObject

@property (nonatomic, assign) int b;

@end

@implementation A

@end

NSMutableArray *arr = [NSMutableArray array];
A *a1 = [[A alloc]init];
a1.b = 10;

A *a2 = [[A alloc]init];
a2.b = 20;

A *a3 = [[A alloc]init];
a3.b = 30;
 
[arr addObject:a1];
[arr addObject:a2];
[arr addObject:a3];

//array 数量
NSLog(@"count %@", [arr valueForKey:@"@count"]);
//array中所有对象a的属性b的最大值
NSLog(@"max %@", [arr valueForKeyPath:@"@max.b"]);
//array中所有对象a的属性b的最小值
NSLog(@"min %@", [arr valueForKeyPath:@"@min.b"]);
//array中所有对象a的属性b和
NSLog(@"sum %@", [arr valueForKeyPath:@"@sum.b"]);
//array中所有对象a的属性b平均数
NSLog(@"avg %@", [arr valueForKeyPath:@"@avg.b"]);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值