title: ObjC internals. Why my duck typing attempt failed?
body: <p>I've tried to use <code>id</code> to create duck typing in objective-c. The concept looks fine in theory but failed in practice. I was unable to use any parameters in my methods. The methods were called but parameters were wrong. I was getting BAD_ACESS for objects and random values for primitives. I've attached a simple example below.</p>
  
  <p><strong>The question:</strong>
  Does any one knows why the methods parameters are wrong?
  What is happening under the hood of the objective-c? </p>
  
  <p>Note: I'm interest in the details. I know how to make the example below work. </p>
  
  <p><strong>An example:</strong>
      I've created a simple class <code>Test</code> that is passed to an other class using property <code>id test</code>.</p>
  
  <pre><code>@implementation Test
  - (void) aSampleMethodWithFloat:(float) f andInt: (int) i {
      NSLog(@"Parameters: %f, %i\n", f, i);
  }
  @end
  </code></pre>
  
  <p>Then in the class the following loop is executed:</p>
  
  <pre><code>for (int i=0; i &lt; 10; ++i) {
      float f=i*0.1f;
      [tst aSampleMethodWithFloat:f andInt:i]; // warning no method found.
  }
  </code></pre>
  
  <p>Here is the output that I'm getting. As you can see the method was called but the parameters were wrong.</p>
  
  <pre><code>Parameters: 0.000000, 0
  Parameters: -0.000000, 1069128089
  Parameters: -0.000000, 1070176665
  Parameters: 2.000000, 1070805811
  Parameters: -0.000000, 1071225241
  Parameters: 0.000000, 1071644672
  Parameters: 2.000000, 1071854387
  Parameters: 36893488147419103232.000000, 1072064102
  Parameters: -0.000000, 1072273817
  Parameters: -36893488147419103232.000000, 1072483532
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I've found out by accident that when I add a declaration of <code>aSampleMethodWith...</code> to the class with <code>for</code> loop the warning disappears and the method on the Test class is called correctly.</p>
  
  <p><strong>Update 2:</strong>
  As pointed out by JeremyP the direct cause of the problem is that the floats are treated as doubles. But anyone knows why? (following the 5why principle :) ).</p>
  
  <p>According to @eman the call is translated to simple C function call and compiler directive to get the <code>SEL</code>. So the @selector gets confused. But why? The compiler have all necessary type informations in the first method call. Does any one knows a good source of information about the Objective-C internals I've search The Objective-C Programming Language but i didn't find the answer.</p>