title: Trying to implement publisher/subscriber in swift
body: <p>I'm actually trying to implement a pub/sub pattern into my Swift projects, I have few <em>Agents</em> (<code>Singleton</code>s) that perform tasks, when their status change they should notify all the listener previously subscribed.</p>
  
  <p>I get the error shaping my protocol, probably it should be conforming to the <code>Equatable</code> protocol, but I cannot see in which way (declaring <code>protocol AvailableChatListener: Equatable</code> resolves nothing).</p>
  
  <p>I know that I could use the Apple's <code>NSNotificationCenter</code> (as nicely points <a href="http://www.raywenderlich.com/90773/introducing-ios-design-patterns-in-swift-part-2" rel="nofollow">here</a>), but it's not a solution completely suitable for my requirements.</p>
  
  <p>Here the code:</p>
  
  <p><strong>AvailableChatListener.swift</strong></p>
  
  <pre><code>protocol AvailableChatListener {
      func onAvailableChatChange()
  }
  </code></pre>
  
  <p><strong>AvailableChatAgent.swift</strong></p>
  
  <pre><code>class AvailableChatAgent {
    // MARK: - Singleton
  
    class var sharedInstance: AvailableChatAgent {
      struct Singleton {
        static let instance = AvailableChatAgent()
      }
      return Singleton.instance
    }
  
    // MARK: - Listeners
  
    private var availableChatListeners = [AvailableChatListener]()
  
    // MARK: - Public Properties
  
    // current Chats
    var currentChats = [Chat]()   {
      didSet  {
        statusChanged()
      }
    }
  
    func startObserving()   {
      // ...
      // perform tasks and change currentChats
      // ...
    }
  
    func stopObserving()    {
      // ...
      // stop tasks
      // ...
    }
  
    func statusChanged()    {
  
      // notify listeners
      for receiver in availableChatListeners {
        receiver.onAvailableChatChange()
      }
    }
  
    // ERROR #1
    func subscribeListener(listener: AvailableChatListener)    {
      availableChatListeners.append(listener)
    }
  
    // ERROR #1
    func unsubscribeListener(listener: AvailableChatListener)  {
  
      // ERROR #2
      if let index = availableChatListeners.indexOf(listener) {
        availableChatListeners.removeAtIndex(availableChatListeners.indexOf(listener)!)
  
        if availableChatListeners.count == 0 {
          self.stopObserving()
        }
      }
    }
  }
  </code></pre>
  
  <p>And here where I use this logic (for example in a TableViewController)</p>
  
  <p><strong>ChatListTableViewController.swift</strong></p>
  
  <pre><code>class ChatListTableViewController: UITableViewController, AvailableChatListener {
  
    // MARK: - Properties
  
    var availableChatAgent = AvailableChatAgent.sharedInstance
  
    // MARK: - ViewDidLoad
  
    override func viewDidLoad() {
      super.viewDidLoad()
  
      availableChatAgent.subscribeListener(self)
    }
  
    // MARK: - Implement AvailableChatListener
  
    func onAvailableChatChange()    {
      // perform task with the new data, for example update UI
    }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Errors that I get:</p>
  
  <ol>
  <li><code>protocol "AvailableChatListener' can only be used as a generic constraint because it has Self or associated type requirements</code></li>
  <li><code>Cannot convert value of type 'AvailableChatListener' to expect argument type '@noescpae(AvailableChatListener) throws -&gt; Bool'</code></li>
  </ol>