title: Key-Value Coding with custom getter, setter and ivar
body: <p>I'm wondering how Key-Value Coding, to access a property value, works in Objective-C when this property has a custom getter, setter and ivar defined. According to <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/SearchImplementation.html#//apple_ref/doc/uid/20000955" rel="nofollow noreferrer">Accessor Search Patterns</a> the runtime will first search for a getter method and fall back to finding an ivar using the reflection string.</p>
  
  <p>According to the search pattern, when neither a getter nor an ivar are found, an exception should be thrown.</p>
  
  <p>However, when I run the following code:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface Class1 : NSObject {
      NSInteger prop;
  }
  
  @property (getter=customGetter,setter=customSetter:) NSInteger prop;
  
  @end
  
  @implementation Class1
  @synthesize prop = customIvar;
  
  @end
  
  int main() {
      Class1 *class1;
  
      // Create and give the properties some values with KVC...
      class1 = [[Class1 alloc] init];
  
      class1.prop = 9;
      NSLog(@"Set value to 9 with direct access");
  
      // Directly access value, should return 9.
      NSLog(@"Direct access: %ld", class1.prop);
  
      // Set with setValue:forKey: to 20.
      NSLog(@"Set value to 20 with KVC");
  
      [class1 setValue:[NSNumber numberWithInt:20] forKey:@"prop"];
  
      // Directly access value.
      NSLog(@"Direct access: %ld", class1.prop);
  
      // Access value using KVC
      NSNumber *propVal = [class1 valueForKey:@"prop"];
  
      NSLog(@"ValueForKey access: %d", [propVal intValue]);
  }
  </code></pre>
  
  <p>I get this output:</p>
  
  <pre><code>Set value to 9 with direct access
  Direct access: 9
  Set value to 20 with KVC
  Direct access: 9
  ValueForKey access: 20
  </code></pre>
  
  <p>It seems that I get two different values: values that are set by directly accessing the property are retrieved when reading directly from the property (<code>9</code>). Values that are set using Key-Value Coding are retrieved by using Key-Value Coding (<code>20</code>).</p>
  
  <p>Does anyone know how this works internally? Is this behaviour expected and am I missing something?</p>