KVC is actually the key coding
1, the assignment fact, two ways
- setValue: forKey to an object's property value assignment
-
Property assignment to a property valueForKey object of: setValue
2, get the value actually two ways
-
valueForKey a property value acquisition targets
-
Property values valueForKeyPath get a property of an object
@interface Cat : NSObject @property (nonatomic,copy) NSString *name; @end @interface Person : NSObject @property (nonatomic,assign) int age; @property (nonatomic, strong) Cat *cat; @end self.p=[[Person alloc]init]; Cat *cat=[[Cat alloc]init]; self.p.cat=cat; [self.p setValue:@12 forKey:@"age"]; [self.p setValue:@"猪" forKeyPath:@"cat.name"]; NSLog(@"--%d--%@",self.p.age,self.p.cat.name);//--12--猪 NSLog(@"--%@--%@",[self.p valueForKey:@"age"],[self.p valueForKeyPath:@"cat.name"]);//--12--猪
3, setValue: forKey: Principle
4, valueForKey: principles
self.p=[[Person alloc]init]; [self.p setValue:@12 forKey:@"age"]; #import "Person.h" @implementation Person -(void)setAge:(int)age{ NSLog(@"1"); } -(void)_setAge:(int)age{ NSLog(@"2"); } @end @interface Person : NSObject { int _age; int _isAge; int age; int isAge; } @end
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
@implementation Person -(int)getAge{ return 1; } -(int)age{ return 2; } -(int)isAge{ return 3; } -(int)_age{ return 4; } @end .h @interface Person : NSObject { int _age; int _isAge; int age; int isAge; }