• KVC profile

    KVCFull name isKey Value CodingDefined in theNSKeyValueCoding.hIn the document, it’s an informal agreement.KVCProvides a mechanism for indirectly accessing its property methods or member variables via a string.

  • KVC common methods

    KVCThe method is shown below:

    • Getting Values
      • valueForKey:

        returnkeyCorresponding attribute value usage[person valueForKey:@"array"]
      • valueForKeyPath:

        Again, returnkeyThe value of the corresponding property, but here you need to passkeyThe path of thevalueForKeyEquivalent to passing in a relative path,valueForKeyPathEquivalent to passing in an absolute path.

        For example:

        Want to getstudentThe 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 callingvalueForKeyThe 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 overridden
      • mutableArrayValueForKey:

        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 oneNSArrayIf you want to change the value of a property of themutableArrayValueForKeyReturn a mutable array and then read and write to the elements of the array
      • mutableArrayValueForKeyPath:Work withvalueForKeyPathThe same absolute path to the incoming key value
      • mutableSetValueForKey:(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 thekeyCorresponding propertiesvalueSet it to a dictionarykeyThe 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 overridden
      • setValue:forKey:Sets the receiver property specified by the given key to the given value.
      • setValue:forUndefinedKey:

        The principle is similarvalueForUndefinedKeyIf 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 basicsetValue: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

      1. To find the firstget<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.)
      2. Check class methodsaccessInstanceVariablesDirectlyWhether to returnYES, if returnYESFind 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
      3. If the retrieved property value is an object pointer, only the result is returned.

        If the value isNSNumberSupported scalar types are stored inNSNumberInstance, and return it. If the result isNSNumberUnsupported scalar type, please convert toNSValueObject and returns the object.
      4. performvalueForUndefinedKeyMethod, which throws an exception by default
      5. The flow chart is as follows:
    • Setting Process Analysis
      1. To find theset<Key>:or_set<Key>Named setters, in that order, call the method and pass in the values if found (converting objects as needed).
      2. If I don’t find a simple setter, butaccessInstanceVariablesDirectlyClass 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
      3. Is called if no setter or instance variable is foundsetValue:forUndefinedKey:Method, and raises an exception by default, but oneNSObjectSubclasses of can propose appropriate behavior.
      4. The flow chart is as follows
    • Custom implementation of KVC

      Making address:Github.com/Bore-TuDou/…