The operation method of KVC is provided by the Nskeyvaluecoding protocol, and NSObject implements this protocol, that is to say, almost all the objects in OBJC support KVC operation, the common KVC operation method is as follows:
-
Dynamic settings: SetValue: Property value Forkey: Property name (for simple Path), SetValue: Property value Forkeypath: Property path (for compound path, for example, person has an account type property, Then Person.account is a composite property)
-
Dynamic read: Valueforkey: Property name, Valueforkeypath: property name (for compound path)
Account.h#import <Foundation/Foundation.h> @interface account:nsobject#pragma Mark-Properties #pragma mark Balance @property (nonatomic,assign) float balance; @end
Account.m#import "Account.h" @implementation account@end
Person.h#import <Foundation/Foundation.h> @class account; @interface person:nsobject{ @private int _ Age;} #pragma mark-Attribute #pragma mark name @property (nonatomic,copy) nsstring *name; #pragma mark account @property (Nonatomic,retain) Acco UNT *account; #pragma mark-public method #pragma mark displays personnel information-(void) showmessage; @end
Person.m#import "Person.h" @implementation person#pragma mark-public method #pragma mark show people Information-(void) showmessage{ NSLog (@ "name=%@,age=%d", _name,_age);} @end
//main.m#import <Foundation/Foundation.h> #import "Person.h" #import "Account.h "Int main (int argc, const char * argv[]) {@autoreleasepool {person *person1=[[person alloc]init]; [Person1 setvalue:@ "Kenshin" forkey:@ "name"]; [Person1 setvalue:@28 forkey:@ "age"];//note even if a private variable can still be accessed [Person1 showmessage]; Results: name=kenshin,age=28 NSLog (@ "Person1 ' name is:%@,age was:%@", Person1.name,[person1 valueforkey:@ "age"]); Results: Person1 ' s name is:kenshin,age is:28 account *account1=[[account Alloc]init]; person1.account=account1;//Note This step must first assign a value to the account attribute, otherwise the following by path assignment cannot succeed, because the account is nil, of course this step can also be written as: [Person1 setValue: Account1 forkeypath:@ "Account"]; [Person1 setvalue:@100000000.0 forkeypath:@ "Account.balance"]; NSLog (@ "Person1 's balance is:%.2f", [[Person1 valueforkeypath:@ "Account.balance"] floatvalue]); Result: Person1 ' s balance is:100000000.00} return 0;}
KVC is relatively simple to use, but how does it find a property to read? Specific lookup rules (assuming you are now reading A with KVC):
-
If the property is set dynamically, the call to the Seta method is preferred, and if there is no method then the search member variable _a is preferred, and if the search member variable A is still not present, then the Setvalue:forundefinedkey of the class will be called if the last search is still not found: Method ( Note that the search process can be set correctly regardless of whether these methods, the member variables are private, or public.
-
If it is a dynamic read property, it is preferable to call the A method (the getter method of property a), if no search is given, the member variable _a will be searched first, if it still does not exist, then the member variable A will be called, if it is still not found, then the valueforundefinedkey of this class is invoked: Method (Note that the search process, regardless of whether these methods, the member variables are private or public can be read correctly);
How to use IOS KVC