title: Can you append to an array inside a dict?
body: <p>In Swift, you can declare a dict where the value is an array type, eg:</p>
  
  <pre><code>var dict: [Int: [Int]] = [:]
  </code></pre>
  
  <p>However, if you assign an array for a given key:</p>
  
  <pre><code>dict[1] = []
  </code></pre>
  
  <p>then it appears that Swift treats the array as immutable.  For example, if we try:</p>
  
  <pre><code>(dict[1] as [Int]).append(0) // explicit cast to avoid DictionaryIndex
  </code></pre>
  
  <p>then we get the error 'immutable value of type [Int] has only mutating members named 'append''.</p>
  
  <hr>
  
  <p>If we explicitly make the array mutable, then the append works, but doesn't modify the original array:</p>
  
  <pre><code>var arr = dict[1]!
  arr.append(0) // OK, but dict[1] is unmodified
  </code></pre>
  
  <hr>
  
  <p><strong>How can you append to an array which is a dict value?</strong></p>
  
  <p>More generally, how can you treat the values of a dictionary as mutable?</p>
  
  <hr>
  
  <p>One workaround is to reassign the value afterwards, but this does not seem like good practice at all:</p>
  
  <pre><code>var arr = dict[1]!
  arr.append(0)
  dict[1] = arr
  </code></pre>