KVC example and fineui online example
KVC-key value Coding allows us to assign values to attribute values in the form of key-value encoding.
Refer to the image on the official website of apple ..
1. KVC
Define a Person class
. H file
1: #import <Foundation/Foundation.h>
2:
3: @interface Person : NSObject
4: {
5: NSString *name;
6: }
7:
8: @end
. M file
1: #import "Person.h"
2:
3: @implementation Person
4:
5: @end
You cannot assign values to names by using common methods to obtain attributes. In this case, you can use KVC to set and set attributes.
1: - (void)viewDidLoad {
2: [super viewDidLoad];
3: // Do any additional setup after loading the view, typically from a nib.
4: Person person=[[Person alloc]init];
5: [person setValue:@"keith" forKey:@"name"];
6: NSLog(@"name is %@",[person valueForKey:@"name"]);
7: }
Output:
21:51:12. 237 KVCtest [543: 10453] name is keith
SetValue: forKey:
ValueForKey:
2. Key Path
What should I do if the class contains other classes and access the attributes of this class. You can use setValue: forKeyPath: And valueForKeyPath to set and obtain
For example, define a car
. H file
#import <Foundation/Foundation.h>@interface Car : NSObject{ NSString *carname;}@end
Change. h of Person
#import <Foundation/Foundation.h>@class Car;@interface Person : NSObject{ NSString *name; Car *hiscar;}-(id)init;@end
. M is
#import "Person.h"#import "Car.h"@implementation Person-(id)init{ if (self=[super init]) { hiscar=[[Car alloc]init]; } return self;}@end
The car instantiation is implemented here. If this is not done, hiscar cannot be assigned a value because it is not initialized during key-value programming.
Call:
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. person * person = [[Person alloc] init]; [person setValue: @ "keith" forKey: @ "name"]; [person setValue: @ "QQ" forKeyPath: @ "hiscar. carname "]; NSLog (@" name is % @, hiscar is % @ ", [person valueForKey: @" name "], [person valueForKeyPath: @" hiscar. carname "]);/* or */Car * car = [[Car alloc] init]; [car setValue: @" BeiKy "forKey: @" carname "]; [person setValue: car forKey: @ "hiscar"]; NSLog (@ "name is % @, hiscar is % @", [person valueForKey: @ "name"], [person valueForKeyPath: @ "hiscar. carname "]);}
Output result
22:45:47. 392 KVCtest [1031: 25564] name is keith, hiscar is QQ
22:45:47. 394 KVCtest [1031: 25564] name is keith, hiscar is BeiKy
3. Type encapsulation and conversion
Modify the Person. h file again
# Import <Foundation/Foundation. h> @ class Car; @ interface Person: NSObject {NSString * name; Car * hiscar; NSInteger age; // Add an age type NSInteger}-(id) init; @ end
So how can this happen during setup?
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. person * person = [[Person alloc] init]; [person setValue: @ "keith" forKey: @ "name"]; [person setValue: @ "QQ" forKeyPath: @ "hiscar. carname "]; [person setValue: @" 24 "forKeyPath: @" age "]; // assign an NSLog (@" name is % @, is % @ old, hiscar is % @ ", [person valueForKey: @" name "], [person valueForKey: @" age "], [person valueForKeyPath: @" hiscar. carname "]);}
The result is:
22:56:08. 873 KVCtest [1086: 27956] name is keith, is 24 old, hiscar is QQ
4. Operation set
Add an otherPerson to Person
#import <Foundation/Foundation.h>@class Car;@interface Person : NSObject{ NSString *name; Car *hiscar; NSInteger age; NSMutableArray *otherPerson;}-(id)init;@end
Attribute. @ sum. attribute, and @ count, @ avg, @ max. @ min
-(void)kvcCollection{ Person *person=[[Person alloc]init]; [person setValue:@"keith" forKey:@"name"]; Person *person1=[[Person alloc]init]; Person *person2=[[Person alloc]init]; Person *person3=[[Person alloc]init]; Person *person4=[[Person alloc]init]; NSMutableArray *others=[[NSMutableArray alloc]initWithObjects:person1,person2,person3,person4, nil]; NSInteger agenum=20; for (Person *p in others) { [self setage:p age:[[NSString alloc]initWithFormat:@"%ld",agenum++]]; } [person setValue:others forKey:@"otherPerson"]; NSLog(@"other's age is %@",[person valueForKeyPath:@"otherPerson.age"]); NSLog(@"the avg age is %@",[person valueForKeyPath:@"otherPerson.@avg.age"]); NSLog(@"the sum age is %@",[person valueForKeyPath:@"otherPerson.@sum.age"]); NSLog(@"the count person is %@",[person valueForKeyPath:@"otherPerson.@count.age"]); NSLog(@"the min age is %@",[person valueForKeyPath:@"otherPerson.@min.age"]); NSLog(@"the max age is %@",[person valueForKeyPath:@"otherPerson.@max.age"]); }-(void)setage:(Person *)person age:(NSString *)age{ [person setValue: age forKey:@"age"];}
Output
23:51:00. 180 KVCtest [1346: 42300] other's age is (
20,
21,
22,
23
)
23:51:00. 183 KVCtest [1346: 42300] the avg age is 21.5
23:51:00. 183 KVCtest [1346: 42300] the sum age is 86
23:51:00. 184 KVCtest [1346: 42300] the count person is 4
23:51:00. 184 KVCtest [1346: 42300] the min age is 20
23:51:00. 184 KVCtest [1346: 42300] the max age is 23
Thank you for your reference.
Copyright Disclaimer: This article will be published at http://www.cnblogs.com/keithmoring/, and you will be welcomed to enjoy the transfer. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!