-
KVC profile
KVC
Full name isKey Value Coding
Defined in theNSKeyValueCoding.h
In the document, it’s an informal agreement.KVC
Provides a mechanism for indirectly accessing its property methods or member variables via a string. -
KVC common methods
KVC
The method is shown below:
-
Getting Values
valueForKey:
returnkey
Corresponding attribute value usage[person valueForKey:@"array"]
valueForKeyPath:
Again, returnkey
The value of the corresponding property, but here you need to passkey
The path of thevalueForKey
Equivalent to passing in a relative path,valueForKeyPath
Equivalent to passing in an absolute path.
For example:
Want to getstudent
The value of an object in a class,[person valueForKeyPath:@"student.subject"]
dictionaryWithValuesForKeys:
Returns a dictionary containing property values identified by each key in a given array.valueForUndefinedKey:
When callingvalueForKey
The method is called if the key is not found in the class’s property. The method can be overridden in the class, or an exception is thrown if it is not overriddenmutableArrayValueForKey:
Returns a mutable array proxy that provides read and write access to the array (containing an immutable array) specified by the given key. It’s useful if oneNSArray
If you want to change the value of a property of themutableArrayValueForKey
Return a mutable array and then read and write to the elements of the arraymutableArrayValueForKeyPath:
Work withvalueForKeyPath
The same absolute path to the incoming key valuemutableSetValueForKey:
(not often used)
Returns a mutable collection agent that provides read and write access to unordered pairs of multiple relationships specified by the given key. Work withmutableArrayValueForKey
mutableSetValueForKeyPath:
(not often used)
Work withvalueForKeyPath
mutableOrderedSetValueForKey:
(not often used)
同mutableArrayValueForKey
mutableOrderedSetValueForKeyPath:
(not often used)
同mutableArrayValueForKeyPath
-
Setting Values
setValue:forKeyPath:
Sets the value of the property identified by the given key path to the given value.
[person setValue:@"Swift" forKeyPath:@"student.subject"];
setValuesForKeysWithDictionary:
Given a dictionary, all of thekey
Corresponding propertiesvalue
Set it to a dictionarykey
The correspondingvalue
setNilValueForKey:
If the callsetValue:forKey:
Method that assigns a scalar value (that is, a simple data type such as int,float, etc.) when value is passed nil. This method is called if overridden in a subclass, and raises an exception if not overriddensetValue:forKey:
Sets the receiver property specified by the given key to the given value.setValue:forUndefinedKey:
The principle is similarvalueForUndefinedKey
If the subclass overrides the method, the subclass’s method is called and an exception is thrown otherwise
-
-
Implementation principle of KVC
Principle exploration flushes the most basic
setValue:forKey:
andvalueForKey:
Everything else is pretty much the same, but the KVC code is closed source so we can only guess what apple’s internal implementation mechanism is by looking at the official documentation-
Value Process Analysis
There are five steps in the official documentation
- To find the first
get<key>
Method if not looking for<key>
The method was not found againis<Key>
methods_<key>
Method if it finds one, go to step 3, otherwise go to step 2. (Note that if it’s NSArray and NSSet there should be two steps below to find out if there’s an original creation method for NSArray and NSSet and if there is one, create one.) - Check class methods
accessInstanceVariablesDirectly
Whether to returnYES
, if returnYES
Find instance variables at once_<key>
._is<Key>
.<key>
oris<Key>
If it can be searched, return the corresponding value; otherwise, go to step 3; otherwise, go to step 4 - If the retrieved property value is an object pointer, only the result is returned.
If the value isNSNumber
Supported scalar types are stored inNSNumber
Instance, and return it. If the result isNSNumber
Unsupported scalar type, please convert toNSValue
Object and returns the object. - perform
valueForUndefinedKey
Method, which throws an exception by default - The flow chart is as follows:
- To find the first
-
Setting Process Analysis
- To find the
set<Key>:
or_set<Key>
Named setters, in that order, call the method and pass in the values if found (converting objects as needed). - If I don’t find a simple setter, but
accessInstanceVariablesDirectly
Class attribute returnYES
, search for a naming rule_<key>
,_is<Key>
,<key>
,is<Key>
Instance variable of. In this order, value is assigned to the instance variable if found - Is called if no setter or instance variable is found
setValue:forUndefinedKey:
Method, and raises an exception by default, but oneNSObject
Subclasses of can propose appropriate behavior. - The flow chart is as follows
- To find the
-
Custom implementation of KVC
Making address:Github.com/Bore-TuDou/…
-