title: Checking if a Swift class conforms to a protocol and implements an optional function?
body: <p>I'm writing a helper function in Swift for use in SpriteKit games that will check if the collision detention has been set up correctly.</p>
  
  <p>I want to check that my GameScene implements the protocol <code>SKPhysicsContactDeletegate</code> and also contains the <code>didBeginContact</code> function.</p>
  
  <p>I have the following code, written with the help of Xcode's auto-complete:</p>
  
  <pre><code>    if GameScene(conformsToProtocol(SKPhysicsContactDelegate)) {
          if GameScene(respondsToSelector(didBeginContact(SKPhysicsContact))) {
              print("Self implements SKPhysicsContactDelegate and didBeginContact appears to be present")
          }
      }
  </code></pre>
  
  <p>Xcode then complains about my <code>conformsToProtocol</code>, complaining that </p>
  
  <blockquote>
    <p>'Argument labels '(_:)' do not match any available overloads'</p>
  </blockquote>
  
  <p>with no suggestion how to fix. It also objects to <code>respondsToSelector</code>, stating that </p>
  
  <blockquote>
    <p>'Cannot convert value of type '(SKPhysicsContact).Type' (aka
    'SKPhysicsContact.Type') to expected argument type 'SKPhysicsContact'</p>
  </blockquote>
  
  <p>How can I check if my GameScene conforms to this protocol and also implements this function?</p>
  
  <p>Edit: Here's my code based upon the answers:</p>
  
  <pre><code>if GameScene.self is SKPhysicsContactDelegate {
      print("Yes it's a delegate")
  }
  </code></pre>
  
  <p>Output: <code>Yes it's a delegate</code></p>
  
  <pre><code>let yy = (GameScene.self as? SKPhysicsContactDelegate)?.didBeginContact
  print("yy is \(yy)")
  </code></pre>
  
  <p>Output: <code>yy is nil</code></p>
  
  <pre><code>if (GameScene.self as? SKPhysicsContactDelegate)?.didBeginContact != nil {
      print("Yes it's a delegate and the function is there")
  }
  </code></pre>
  
  <p>No output.</p>