title: Check if AnyObject is of generic type in Swift
body: <p>Let's say that I have generic class <em>Bus</em>:</p>
  
  <pre><code>class Bus&lt;T&gt; {
      func doSomething() {}
  }
  </code></pre>
  
  <p>and I can create instance of it:</p>
  
  <pre><code>var myBus = Bus&lt;String&gt;()
  </code></pre>
  
  <p>Now I have a function that takes one parameter of the <code>AnyObject</code> and tests it's type:</p>
  
  <pre><code>func checkType(object: AnyObject) {
      if let foo = object as? String {
          println("string")
      }
  }
  </code></pre>
  
  <p>My problem is that I can't see a way to check if <code>object</code> is of type <code>Bus</code> and run function <code>doSomething()</code> if it is of type <code>Bus</code>. Any help would be appreciated.</p>
  
  <hr>
  
  <p><strong>Edit</strong>: Protocols also don't seem to solve this the way they should.</p>
  
  <pre><code>import Foundation
  
  @objc protocol BusProtocol {
      func doSomething() -&gt; Void
  }
  
  class Bus&lt;T&gt; : BusProtocol {
      func doSomething() -&gt; Void {
          println("asdf")
      }
  }
  
  func checkType(object: AnyObject) {
      if let foo = object as? Bus&lt;AnyObject&gt; {
          foo.doSomething() // no match
      }
      if let foo = object as? Bus&lt;Any&gt; {
          foo.doSomething() // no match
      }
      if let foo = object as? Bus&lt;String&gt; {
          foo.doSomething() // prints "asdf"
      }
      if let foo = object as? BusProtocol {
          foo.doSomething() // SIGABRT -- -[SwiftObject doesNotRecognizeSelector:]
      }
  }
  
  checkType(Bus&lt;String&gt;())
  </code></pre>