IOS KVC Overview
The basic concept of KVCKVC --> WhatKVC is a way to indirectly change the object state (or attribute value:Key-value codingKVC for short. the main essential feature is that a string is used to identify the property variables of an object, and this identifier can be used to change the State (or attribute value) of an object. This indirect manifestation is that a string is used to identify the property ,? Instead of calling? Access? Method or direct access to instance variables. What is the KVC mechanism? Supported objects? Supports scalar and struct types? Non-object type will be? Automatic packing and unpacking. Key & Key Path
What is the Key? A string? Used to identify an object? In? Above? A specified property .? Average? What is the access to an object corresponding to a key? Method or instance variable. The key must be an ASCII code ,? Generally? Small writing? The start of the parent node. It cannot contain spaces.
A key path is a string of dot separated keys that is used to specify a sequence of object properties to traverse. the property of the first key in the sequence is relative to the aggreger, and each subsequent key is evaluated relative to the value of the previous property.
What is the Key Path? A forward by vertex? Split rows? A string consisting of a series of keys
Key Path concept and Representation: It can be represented by dots between objects and different variable names.
Note:
The depth of the Key Path is arbitrary, depending on the complexity of the relationship between objects.
Basic KVC usage
-(Void) setValue :( id) value forKey :( NSString *) key-(id) valueForKey :( NSString *) key // uses the key as the identifier to obtain the corresponding property value-(void) setValue :( id) value forKey :( NSString *) key // use the key as the identifier to set its corresponding property value-(id) valueForUndefinedKey :( NSString *) key-(void) setNilValueForKey :( NSString *) key
The call mechanism of KVC: valueForKey: First searches for the getter method named by the parameter name (in the format of-key or isKey). If yes, ze calls this method; if such a getter method is not found, it searches for instance variables whose names are in the _ key or key format within the object, and then returns the result. the setValue: forKey: mechanism is similar to valueForKey. it first finds the setter method of the parameter name, and completes the setting if it finds it. If the setter method is not found, it finds the instance variable in the class in the format of _ key or key, then assign value to it. use of KVC in a set
@ Avg
NSnumber *transactionAverage = [transactions valueForKeyPath:@@avg.amount];
@ Count
NSNumber *numberOfTransactions = [transactions valueForKeyPath:@@count]?;
@ Max
NSDate *latestDate = [transactions valueForKeyPath:@@max.date];
@ Min
NSDate *earliestDate = [transactions valueForKeyPath:@@min.date];
@ Sum
NSNumber *amountSum = [transactions valueForKeyPath:@@sum.amount];
The method of using the key value and Key Path is relatively simple. I will use a KVC in the set to demonstrate it directly.
Create a QYPerson class that inherits from NSObject
#import
@class QYCourse;@interface QYPerson : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) QYCourse *course;@property (nonatomic, assign) NSInteger point;@property (nonatomic, strong) NSArray *persons;@end
In the main. m function
int main(int argc, const 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:@78 forKey:@point]; [p2 setValue:@87 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:@persons.@count]); NSLog(@the max achievement :%@,[person valueForKeyPath:@persons.@max.point]); NSLog(@the min achievement :%@,[person valueForKeyPath:@persons.@min.point]); NSLog(@the avg achievement :%@,[person valueForKeyPath:@persons.@avg.point]); } return 0;}
The output result is as follows:
2015-07-29 15:25:48.856 KVCDemo[2968:1026943] other persons' achievement info:( 78, 87, 98)2015-07-29 15:25:48.856 KVCDemo[2968:1026943] all persons' number is 32015-07-29 15:25:48.856 KVCDemo[2968:1026943] the max achievement :982015-07-29 15:25:48.856 KVCDemo[2968:1026943] the min achievement :782015-07-29 15:25:48.876 KVCDemo[2968:1026943] the avg achievement :87.666666666666666666666666666666666666