title: Cannot invoke method with argument list of type KeyType in Swift
body: <p>I am working on custom message dispatcher in Swift. I want to have dispatcher which can:</p>
  
  <ul>
  <li>dispatch enums as events or whatever what is <code>Hashable</code>.</li>
  <li>Every object which implements one protocol might be added as listeners</li>
  </ul>
  
  <p>Here is the code:</p>
  
  <pre><code>import Foundation
  
  protocol EventDispatcherProtocol: class {
      typealias T: Hashable
      func dispatcherDidDispatchEvent(event: T)
  }
  
  class EventListenerWrapper&lt;T: EventDispatcherProtocol&gt; {
      weak var object: T?
  
      init(_ object: T) {
          self.object = object
      }
  }
  
  class EventDispatcher&lt;KeyType: Hashable, U: EventDispatcherProtocol&gt; {
      private var objects = Dictionary&lt;KeyType, [EventListenerWrapper&lt;U&gt;]&gt;()
  
      func add(#listener: U, forEvent event: KeyType) {
          let wrapper = EventListenerWrapper(object: listener)
  
          // get all wrappers
          if let storedWrappers = objects[event] {
              // check if there is such objects already wrapped for this type of event
              var found = false
              for storedWrapper in storedWrappers {
                  if let storedObject = storedWrapper.object
                  where (storedObject as! AnyObject) === (wrapper.object as! AnyObject) {
                      // Found it!
                      found = true
                      break
                  }
              }
  
              if found == false {
                  // If not found then add the wrapper for this event
                  var updatedWrappers = storedWrappers
                  updatedWrappers.append(wrapper)
                  self.objects[event] = updatedWrappers
              }
  
          } else {
              self.objects[event] = [wrapper]
          }
      }
  
      func dispatch(event: KeyType) {
          // Enumerate through wrappers
          if let storedWrappers = objects[event] {
              var idxToRemove = [Int]()
  
              var currentIdx = 0
              for storedWrapper in storedWrappers {
                  // if there is object to dispatch - dispatch message
                  // otherwise add index of this object to remove wrapper later
                  if let storedObject = storedWrapper.object {
  //                    storedObject.dispatcherDidDispatchEvent(event) /// &lt;- cannot invoke 'this method'  with an argument list of type '(KeyType)'
                  } else {
                      idxToRemove.append(currentIdx)
                  }
  
                  currentIdx++
              }
  
              // enumerate through reversed array of indexes to remove
              // and remove items, update wrappers at the end.
              idxToRemove = idxToRemove.reverse()
              var updatedWrappers = storedWrappers
              for idx in idxToRemove {
                  updatedWrappers.removeAtIndex(idx)
              }
  
              objects[event] = updatedWrappers
          }
      }
  }
  
  
  
  // EXAMPLE
  
  enum CustomEvent {
      case Start, Stop
  }
  
  class CustomListener: EventDispatcherProtocol {
      func dispatcherDidDispatchEvent(event: CustomEvent) {
          println("CustomListener heard that event! \(event)")
      }
  }
  
  // Create listeners and wrap them
  var listener1 = CustomListener()
  var listener2 = CustomListener()
  
  // Create dispatcher
  var dispatcher = EventDispatcher&lt;CustomEvent, CustomListener&gt;()
  dispatcher.add(listener: listener1, forEvent: CustomEvent.Start)
  dispatcher.add(listener: listener2, forEvent: CustomEvent.Stop)
  
  dispatcher.dispatch(CustomEvent.Start)
  dispatcher.dispatch(CustomEvent.Stop)
  </code></pre>
  
  <p>Unfortunately there is an error in <code>object.dispatcherDidDispatchEvent(event)</code> call. I cannot understand what is wrong with this. It looks correctly for me.</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>EDIT</strong> - Working solution after @Tobol suggestion</p>
  
  <pre><code>import Foundation
  
  protocol EventDispatcherProtocol {
      typealias T: Hashable
      func dispatcherDidDispatchEvent(event: T)
  }
  
  class EventListenerWrapper&lt;T: EventDispatcherProtocol&gt; {
      var object: T?
  
      init(_ object: T) {
          self.object = object
      }
  }
  
  class EventDispatcher&lt;U: EventDispatcherProtocol&gt; {
      typealias KeyType = U.T
      private var objects = Dictionary&lt;KeyType, [EventListenerWrapper&lt;U&gt;]&gt;()
  
      func add(#listener: U, forEvent event: KeyType) {
          let wrapper = EventListenerWrapper(listener)
          // get all wrappers
          if let storedWrappers = objects[event] {
              // check if there is such objects already wrapped for this type of event
              // and if not found add the wrapper for this event
              if storedWrappers.filter({($0.object as? AnyObject) === (listener as! AnyObject) }).count == 0 {
                  objects[event] = storedWrappers + [wrapper]
              }
  
          } else {
              objects[event] = [wrapper]
          }
      }
  
      func dispatch(event: KeyType) {
          // Enumerate through wrappers
          if var storedWrappers = objects[event] {
              // Enumerate through stored wrappers and get indexes of objects to remove
              var indexesToRemove = [Int]()
              var currentIndex = 0
              storedWrappers.filter({
                  var match = $0.object == nil
                  if match { indexesToRemove.append(currentIndex) }
                  currentIndex++
                  return match
              })
  
              // Reverse array of indexes to make removing process easier
              for index in indexesToRemove.reverse() {
                  storedWrappers.removeAtIndex(index)
              }
  
              // Dispatch message on existing objects
              for wrapper in storedWrappers {
                  wrapper.object!.dispatcherDidDispatchEvent(event)
              }
  
              objects[event] = storedWrappers
          }
      }
  }
  
  
  
  // EXAMPLE
  enum CustomEvent {
      case Start, Stop
  }
  
  class CustomListener: EventDispatcherProtocol {
      func dispatcherDidDispatchEvent(event: CustomEvent) {
          println("CustomListener heard that event! \(event)")
      }
  }
  
  // Create listeners and wrap them
  var listener1 = CustomListener()
  
  // Create dispatcher
  var dispatcher = EventDispatcher&lt;CustomListener&gt;()
  dispatcher.add(listener: listener1, forEvent: CustomEvent.Start)
  dispatcher.dispatch(CustomEvent.Start)
  </code></pre>