The basic concept of KVCKVC-–>what
- KVC is a way to indirectly change an object's state (or attribute value):key-value coding is called KVC.
- The main feature is the use of a string to identify the object's property variables, and can use this identity to change the state of the object (or attribute value)
- This indirect behavior is to identify attributes by string, rather than by using the accessor method or accessing the instance variable directly.
- The KVC mechanism not only supports objects, but also supports scalar and struct types, and the types of non-objects are automatically boxed and unpacking.
Key & Key Path
Key (Key) is a string? is used to identify the object? A specified property. A key corresponds to an object's access? Method or instance variable. The key must be an ASCII code, usually in the case of lowercase letters. Start with, you cannot include spaces.
A key path is a string of dot separated keys, which is used to specify a sequence of the object properties to traverse. The property of the first key in the sequence are relative to the receiver, and all subsequent key is evaluated relative t o The value of the previous property.
Key path? A string of a series of keys that are split by a point in line?
the concept and representation of key paths: they can be represented by dots separated between objects and different variable names.
Note:
The depth of the key path is arbitrary, depending on the complexity of the relationship between the objects
KVC Basic Usage
- (void)setValue:(id)value forKey:(NSString *)key - (id)valueForKey:(NSString *)key - (id)valueForKey:(NSString//以 key 作为标示符,获取其对应的属性值 - (void)setValue:(id)value forKey:(NSString//以 key 作为标示符设置其对应的属性值 - (id)valueForUndefinedKey:(NSString *)key - (void)setNilValueForKey:(NSString *)key
KVC Call Mechanism:
- Valueforkey: The Getter method named with the parameter name (formatted as-key or iskey) is searched first, and if found, the method is called; If no such getter method is found, it will look inside the object for an instance variable with the name format _ key or key, and then return.
- Setvalue:forkey: The mechanism is similar to Valueforkey. It will first look up the setter method of the parameter name and, if found, complete the setup; If no setter method is found, the instance variable with the name format _ key or key is found directly in the class, and value is assigned to it.
The use of KVC in collections
@avg
*transactionAverage = [transactions valueForKeyPath:@"@avg.amount"];
@count
*numberOfTransactions = [transactions valueForKeyPath:@"@count"]?;
@max
*latestDate = [transactions valueForKeyPath:@"@max.date"];
@min
*earliestDate = [transactions valueForKeyPath:@"@min.date"];
@sum
*amountSum = [transactions valueForKeyPath:@"@sum.amount"];
The method of using key and key paths is simple, and I will use a KVC in the collection to demonstrate
Create a Qyperson class that inherits from NSObject
#import <Foundation/Foundation.h> @class qycourse; @interface qyperson : nsobject @property(nonatomic,Strong)NSString*name;@property(nonatomic,Strong) Qycourse *course;@property(nonatomic,Assign)NsintegerPoint@property(nonatomic,Strong)Nsarray*persons;@end
In the MAIN.M function
intMainintargcConst Char* argv[]) {@autoreleasepool {Qyperson *person = [[Qyperson alloc] init]; Qyperson *P1 = [[Qyperson alloc] init]; Qyperson *P2 = [[Qyperson alloc] init]; Qyperson *P3 = [[Qyperson alloc] init]; [P1 setvalue:@" the"forkey:@"Point"]; [P2 setvalue:@" the"forkey:@"Point"]; [P3 setvalue:@"98"forkey:@"Point"];Nsarray*array = @[p1,p2,p3]; [Person Setvalue:array forkey:@"Persons"];NSLog(@"Other persons ' achievement info:%@", [Person valueforkeypath:@"Persons.point"]);NSLog(@"All persons ' number is%@", [Person valueforkeypath:@"[email protected]"]);NSLog(@"The max Achievement:%@", [Person valueforkeypath:@"[email protected]"]);NSLog(@"The min Achievement:%@", [Person valueforkeypath:@"[email protected]"]);NSLog(@"The AVG Achievement:%@", [Person valueforkeypath:@"[email protected]"]); }return 0;}
The results of the output are as follows:
-- -- in the: -:48.856kvcdemo[2968:1026943] Other persons ' Achievement info: ( +, the,98) -- -- in the: -:48.856kvcdemo[2968:1026943] All persons ' Number is 3 -- -- in the: -:48.856kvcdemo[2968:1026943] theMax Achievement:98 -- -- in the: -:48.856kvcdemo[2968:1026943] theMin Achievement: + -- -- in the: -:48.876kvcdemo[2968:1026943] theAvg Achievement:87.666666666666666666666666666666666666
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS KVC Overview