title: Objective-c protocol to Swift class. unrecognized method
body: <p>I'm trying to migrate some code from Objective-c to Swift, but i have problems at the beggining when i want to conform a Swift class with a Objective-c protocol and access this class from a objetive-c class. I'm doing something wrong, but i don't see it.</p>
  
  <p>Objective-c protocol and obj-c class for testing (test_class.h)</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @protocol test_delegate
  
  -(void)returnData:(NSString*)data InMethod:(NSString*)method;
  @end
  
  @interface test_class : NSObject
  
  @property (weak, nonatomic) id&lt;test_delegate&gt; delegate;
  -(void)sendData:(NSString * )data;
  @end
  </code></pre>
  
  <p>Objective-c implementation</p>
  
  <pre><code>#import "test_class.h"
  
  @implementation test_class
  
  -(id) init{
      self = [super init];
      if (self != nil){
          self.delegate= nil;
      }
      return self;
  }
  -(id) initWithDelegate:(id&lt;test_delegate&gt;) delegate{
      self = [super init];
      if (self != nil){
          self.delegate = delegate;
  
      }
      return self;
  }
  
  
  -(void)sendData:(NSString *)data{
  
      [self.delegate returnData:data InMethod:@"method test"];
  }
  @end
  </code></pre>
  
  <p>Objetive-c brigde file</p>
  
  <pre><code>//
  //  Use this file to import your target's public headers that you would like to expose to Swift.
  //
  
  #import "test_class.h"
  </code></pre>
  
  <p>Swift file (FirstViewController.swift)</p>
  
  <pre><code>import UIKit
  
  class FirstViewController: UIViewController, test_delegate {
  
      var test : test_class!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          test = test_class()
          test.delegate=self
          test.sendData("show in log")
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func returnData(data: String!, inMethod method: String!) {
          NSLog(data)
      }
  
  
  }
  </code></pre>
  
  <p>Finally the error compile gives me</p>
  
  <pre><code>2015-01-27 08:21:05.787 Test[4566:51164] -[Test.FirstViewController returnData:InMethod:]: unrecognized selector sent to instance 0x7fa6abd0aa20
  2015-01-27 08:21:05.792 Test[4566:51164] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.FirstViewController returnData:InMethod:]: unrecognized selector sent to instance 0x7fa6abd0aa20'
  </code></pre>
  
  <p>This code, using only Obj-c classes works fine. What I'm doing wrong? </p>
  
  <p>Using Xcode 6.1.1 and deployment target iOS 8.0</p>
  
  <p>Thank you in advance.</p>
  
  <p><strong>UPDATE:</strong>
  Swift has a weird behaviour when implement some method from a objective-c protocol: param's name must start with a tiny letter at the the begginig, else, compiler give you a compile-time error, and if not, give you a runtime error. </p>
  
  <p>The unique solution I've found, is modify objective-c protocol method definition this way:</p>
  
  <pre><code>@protocol test_delegate
  
  -(void)returnData:(NSString*)data InMethod:(NSString*)method;
  @end
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>@protocol test_delegate
  
  -(void)returnData:(NSString*)data inMethod:(NSString*)method;
  @end
  </code></pre>
  
  <p>Doing this change, it runs perfectly.</p>
  
  <p>If anyone has an answer for this behaviour, is welcome to post and explain why this happens.</p>
  
  <p><strong>UPDATE 2:</strong>
  Thank you to <a href="https://stackoverflow.com/users/538458/adam-freeman">@Adam Freeman</a> to point out another weird issue of Swift with classes names and variables names. Copied (with his permission) the code here:</p>
  
  <p>Another thing to watch out for that is if your protocol delegate method takes its class as one of its parameters. Using this example:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @class TestClass;
  @protocol TestDelegate
  -(void)TestClass:(TestClass*)TestClass returnData:(NSString*)data inMethod:(NSString*)method;
  @end
  
  @interface TestClass : NSObject
  
  @property (weak, nonatomic) id&lt;TestDelegate&gt; delegate;
  -(void)sendData:(NSString * )data;
  @end
  </code></pre>
  
  <p>This will cause problems with <b>TestClass</b> being found in your Swift code.  The fix is:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @class TestClass;
  @protocol TestDelegate
  -(void)testClass:(TestClass*)testClass returnData:(NSString*)data inMethod:(NSString*)method;
  @end
  
  @interface TestClass : NSObject
  
  @property (weak, nonatomic) id&lt;TestDelegate&gt; delegate;
  -(void)sendData:(NSString * )data;
  @end
  </code></pre>
  
  <p>Incidentally <strong>the Swift spec states that such things as classes and delegates should start with an upper case letter and methods and parameter names should start with a lower case letter.</strong></p>