title: How do I expose a Swift Dictionary as a typed sequence of key-value pairs?
body: <p>Say I have a class along the lines of:</p>
  
  <pre><code>class Foo&lt;K, V&gt; {
      var dict: [K:V] = [:]
  }
  </code></pre>
  
  <p>I want to let callers iterate over the key-value pairs in the dictionary, without providing direct access to the dictionary itself. So a caller would be able to write something like:</p>
  
  <pre><code>val foo: Foo&lt;Bar, Qux&gt; = ...
  
  for (b, q) in foo.asSequence() {
     ...
  }
  </code></pre>
  
  <p>Now, since <code>Dictionary</code> conforms to <code>SequenceType</code>, coming from other languages, I would expect to just add a method to Foo along the lines of:</p>
  
  <pre><code>    func asSequence() -&gt; SequenceType&lt;K, V&gt; {
          return dict
      }
  </code></pre>
  
  <p>However, Swift protocols don't take generic parameters and <code>SequenceType</code> is one of those annoying types that "can only be used as a generic constraint because it has Self or associated type requirements".</p>
  
  <p>I could probably figure out how to wrap <code>dict</code> in something that conforms to <code>SequenceType</code> (or make <code>Foo</code> itself conform to <code>SequenceType</code>) but I don't want to start messing around with generators and whatnot until I absolutely have to — this seems like it should be a one-liner if I just understood the available options better. (And it's not clear to me whether that would be enough to get the generics to work such that <code>(b, q)</code> has the right types, anyway.)</p>