title: Swift Extension fails adding overloaded methods to Objective-C class
body: <p>If a Swift extension is used to add overloaded methods to an Objective-C class, it appears to only call the first method, producing unexpected behaviour or a crash at runtime. An Extension on a Swift class (as opposed to Objective-C) works correctly. I would like to establish if there's any workarounds to this and confirm that this is a bug that I should report to Apple. Given that Objective-C does not support overloaded methods while Swift does, I can imagine that mixing the two is a recipe for problems.</p>
  
  <p>The classes getting the extension are both empty except for the Objective-C class having an init function. ObjClass is declared within .h and .m files and imported using the Swift bridging header. The original SwiftClass is defined within the code sample along with the Swift extensions:</p>
  
  <pre><code>extension ObjClass {
      func log(param: Int32)    { NSLog("ObjClass-&gt;Int32: " + String(param)) }
      func log(param: String)   { NSLog("ObjClass-&gt;String: " + param) }
      func log(param: ObjClass) { NSLog("ObjClass-&gt;ObjClass") }
  }
  
  class SwiftClass {
  }
  
  extension SwiftClass {
      func log(param: Int32)    { NSLog("SwiftClass-&gt;Int32: " + String(param)) }
      func log(param: String)   { NSLog("SwiftClass-&gt;String: " + param) }
      func log(param: ObjClass) { NSLog("SwiftClass-&gt;ObjClass") }
  }
  </code></pre>
  
  <p>Now call the overloaded methods with a string, int and object:</p>
  
  <pre><code>var objClass = ObjClass()
  objClass.log(10)
  objClass.log("string")
  objClass.log(objClass)
  
  var swiftClass = SwiftClass()
  swiftClass.log(10)
  swiftClass.log("string")
  swiftClass.log(objClass)
  </code></pre>
  
  <p>For ObjClass, all the method calls are made to the first method with type Int32. Through use of <a href="https://stackoverflow.com/questions/24188405/get-object-pointers-memory-address-as-a-string">COpaquePointer</a>, I can see this is related to the object pointer but slightly different.</p>
  
  <pre><code>ObjClass-&gt;Int32: 10
  ObjClass-&gt;Int32: 1881422800
  ObjClass-&gt;Int32: 1879122512
  </code></pre>
  
  <p>Swift Extension on a Swift class works as expected:</p>
  
  <pre><code>SwiftClass-&gt;Int32: 10
  SwiftClass-&gt;String: string
  SwiftClass-&gt;ObjClass
  </code></pre>
  
  <p>Hopefully this can be supported at some point. Until then, I believe it should fail at compile time rather than give unexpected behaviour or a crash. Does anyone have a workaround to suggest for this?</p>
  
  <p>I know it's calling the first method as if I swap the ObjClass extension methods, like so:</p>
  
  <pre><code>extension ObjClass {
      func log(param: String)   { NSLog("ObjClass-&gt;String: " + param) }
      func log(param: Int32)    { NSLog("ObjClass-&gt;Int32: " + String(param)) }
      func log(param: ObjClass) { NSLog("ObjClass-&gt;ObjClass") }
  }
  </code></pre>
  
  <p>Then the call to <code>objClass.log(str)</code> works fine but the call to <code>objClass.log(10)</code> produces:</p>
  
  <pre><code>main(1): EXC_BAD_ACCESS (code=1, address=0xa)
  </code></pre>