What is theKVC?

KVC is short for key-value Coding, known as key-value Coding. It is a mechanism enabled by the NSKeyValueCoding informal protocol that allows objects to access their properties indirectly, complementing the direct access provided by instance variables and their associated accessor methods.

KVC main API analysis

Value and set value

- (void)setValue:(nullable id)value forKey:(NSString *)key; - (nullable id)valueForKey:(NSString *)key; // set the value to keyPath in valueForKeyPath - (nullable id)valueForKeyPath:(NSString *)keyPath; //keyPath - (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;Copy the code

Results the following

Other apis

/ / the default returns YES, said if not found the Set < Key > method, according to _key, _iskey, Key, iskey sequential search members, Set to NO don't search + (BOOL) accessInstanceVariablesDirectly; //KVC provides an API for verifying the correctness of attribute values, which can be used to check if the value of a set is correct, make a replacement value for an incorrect value, or reject a new value and return the cause of the error. - (BOOL)validateValue:(inout id __nullable * __nonnull)ioValue forKey:(NSString *)inKey error:(out NSError **)outError; // This is the set operation API, there are a series of such apis, if the property is an NSMutableArray, then you can use this method to return. - (NSMutableArray *)mutableArrayValueForKey:(NSString *)key; // If the Key does not exist and KVC cannot find any fields or attributes related to the Key, this method is called. By default, an exception is thrown. - (nullable id)valueForUndefinedKey:(NSString *)key; // Same as the previous method, but this method is set. - (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key; // if you pass nil to Value during the SetValue method, this method is called - (void)setNilValueForKey:(NSString *)key; // Input a set of keys, return the values corresponding to the keys, and then return the dictionary, which is used to transfer the Model to the dictionary. - (NSDictionary<NSString *, id> *)dictionaryWithValuesForKeys:(NSArray<NSString *> *)keys;Copy the code

The principle of analysis

So we saw thatKVCSome common apis, then inSet the value and its valueWhat does the underlying iOS do?We see this in the codeAPIisFoundationUnder the framework, due toFoundationIs not open source, so we can’t see the underlying implementation through source code. But we can passApple Official DocumentationTo look at it. Check out apple’s documentationKey-Value Coding Programming Guide

Analysis of KVC value principle

We know from the official documentation that when we set a value by setValue:forKey:, the steps are as follows

  • Step one: Look for these three firstsetter:Methods are as follows:set<Key>->_set<Key>->setIs<Key>, including<Key>isThe name of the member variable with a capital letter, as in this articlenameThe order issetName->_setName->setIsName. When you find these threesetter, then the assignment, if not found, enterStep 2.
  • Step 2Judge:+ (BOOL)accessInstanceVariablesDirectlyWhether the function returns YES. If YES is returned, the_key->_iskey->key->iskeySearch for members in the order of, find any one, then assign, otherwise enterStep 3; If NO is returned, enter directlyStep 3.
  • Step 3If neither setter method nor instance variable is found, the system executes the objectSetValue: forUndefinedKey:Function, which throws an exception by default, so we useKVCTo implement when parsing model dataSetValue: forUndefinedKey:Function, otherwise it crashes.
  • Set value to summarize

Analysis of KVC value principle

When we call valueForKey:, the internal flow is as follows:

  • Step one: callgetterMethod, called in the following order:get<Key> -> <key> -> is<Key> -> _<key>, take name as an example,getName -> name -> isName -> _name, returns the corresponding value (possibly an object) if it is found, or enters if it cannot be foundStep 2
  • Step 2Judge:+ (BOOL)accessInstanceVariablesDirectlyDoes the function return YES, and if YES, the member variables are accessed in the following order:_<key>->_is<Key>-><key>-> is<Key>For example, name is_name->_isName->name-> isName, returns the corresponding value (possibly an object) if it can’t find, entersStep 3; If NO is returned, the entry is cut offStep 3.
  • Step 3: If no, the call is invokedValueForUndefinedKey:Throw an exception.
  • Values summarize: