title: Two class methods, same name, different signatures: How to force compiler to use intended one?
body: <p>How do I force the compiler to select the desired method from a set of <em>class</em> methods sharing the same name?</p>
  
  <pre><code>/* Use +[MyClass port](UInt16 (*)(id, SEL),
   * not +[NSPort port](NSPort *(*)(id, SEL)). */
  UInt16 port = [[self class] port];
  </code></pre>
  
  <p>I have an Objective-C class with a class method:</p>
  
  <pre><code>+ (UInt16)port;
  </code></pre>
  
  <p><code>NSPort</code> has a convenience constructor whose signature conflicts with this:</p>
  
  <pre><code>+ (NSPort *)port;
  </code></pre>
  
  <p>Sending <code>+port</code> to my class results in a compiler warning:</p>
  
  <pre><code>UInt16 port = [[self class] port];
      W: Multiple methods named '+port' found
      W: Using '+(NSPort *)port'
      W: Also found '+(UInt16)port'
  </code></pre>
  
  <p>Fail: The compiler has chosen the wrong method signature.</p>
  
  <p>Type inference fail: Using <code>[[(MyClass *)self class] port]</code> does not cadge it into using the right method.</p>
  
  <p><em><strong>ETA:</em></strong> Here is the workaround I am using for now:</p>
  
  <pre><code>#import &lt;objc/runtime.h&gt;
  
  Class c = [self class];
  SEL s = @selector(port);
  typedef UInt16 (*PortIMP)(id, SEL);
  PortIMP MyClassGetPort = (PortIMP)class_getMethodImplementation(c, s);
  UInt16 port = MyClassGetPort(c, s);
  </code></pre>
  
  <p>It is good in that:</p>
  
  <ul>
  <li>It handles dispatching to any subclass implementation correctly.</li>
  <li>It is confined to the implementation file, so this ugliness is not inflicted on anyone but the implementer.</li>
  </ul>
  
  <p>It is bad in that it won't help anyone else who wants to call the method.</p>