title: How to create class methods that conform to a protocol shared between Swift and Objective-C?
body: <p>I've been learning Swift lately.</p>
  
  <p>I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages.</p>
  
  <p>The program calculates a large array of prime numbers.</p>
  
  <p>I defined a protocol that both the Swift and the Objective-C version of the calculate object are supposed to conform to. </p>
  
  <p>The objects are both singletons, so I created a typical singleton access method in Objective-C:</p>
  
  <pre><code>+ (NSObject &lt;CalcPrimesProtocol&gt; *) sharedInstance;
  </code></pre>
  
  <p>The whole protocol looks like this:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  @class ComputeRecord;
  
  typedef void (^updateDisplayBlock)(void);
  typedef void (^calcPrimesCompletionBlock)(void);
  
      @protocol CalcPrimesProtocol &lt;NSObject&gt;
  
  - (void) calcPrimesWithComputeRecord: (ComputeRecord *) aComputeRecord
                withUpdateDisplayBlock: (updateDisplayBlock) theUpdateDisplayBlock
                    andCompletionBlock: (calcPrimesCompletionBlock) theCalcPrimesCompletionBlock;
  
      @optional //Without this @optional line, the build fails.
      + (NSObject &lt;CalcPrimesProtocol&gt; *) sharedInstance;
  
      @end
  </code></pre>
  
  <p>The Objective-C version of the class implements the methods exactly as defined above, no worries.</p>
  
  <p>The swift version has a method:</p>
  
  <pre><code>  class func sharedInstance() -&gt; CalcPrimesProtocol
  </code></pre>
  
  <p>However, if I make that method a required method of the protocol, I get a compiler error "Type "CalcPrimesSwift does not conform to the protocol 'CalcPrimesProtocol'.</p>
  
  <p>If I mark the singleton class method sharedInstance as optional in the protocol, however, it works, and  I can invoke that method on either my Swift class or my Objective-C class.</p>
  
  <p>Did I miss some subtlety in the definition of my Swift class method? It seems unlikely, given that I'm able to invoke the sharedInstance() class method on either my Swift class or my Objective-C class.</p>
  
  <p>You can download the project from Github and check it out if you'd like. It's called <a href="https://github.com/DuncanMC/SwiftPerformanceBenchmark.git"><strong>SwiftPerformanceBenchmark</strong></a>. (link)</p>