title: Appending Multidimensional Dictionary
body: <p>I have a multidimensional dictionary and I am trying to add data into it without deleting data, but overwrite if duplicate.</p>
  
  <pre><code>var items = [Int: AnyObject]()
  
  var IdsAndDetails = [String:AnyObject]()
  let index = 3 // static for test
  
  ...
  for result in results {
       // result is String (result also indicates itemId)
  
       let details = self.IdsAndDetails[result]  
       // details is AnyObject like [String:String]
  
      if let itemDetails = details {        
           // Here I want to append data into 'items' variable
  
           // This doesn't work: (Error-1)
            self.items[index]![result] = itemDetails
  
      }
  </code></pre>
  
  <p>(Error-1):</p>
  
  <blockquote>
    <p>Cannot assign to immutable expression to type AnyObject.</p>
  </blockquote>
  
  <hr>
  
  <p>However, if I try like, it works but it's not the approach I want. It's re-creating the dictionary. Instead, I want to append the data.</p>
  
  <pre><code>      self.items = [
           index : [result : itemDetails]
        ]
  </code></pre>
  
  <p>The structure of dictionary I want to get in the end is:</p>
  
  <pre><code>items = [
      index : [
          "id1" : ["key": "value", "key": "value"],
          "id2" : ["key": "val", "key": "val"],
          ],
      index : [
          "id3" : ["key": "val", "key": "val"],
          "id4" : ["key": "val", "key": "val"],
          "id5" : ["key": "val", "key": "val"],
      ]
  ]
  
  // index is Integer
  // [Key:Value] is [String:String] - itemDetails equal to all `[key:value]`s
  // 'id' is also String
  </code></pre>
  
  <hr>
  
  <p>Update: I also tried, but no luck</p>
  
  <pre><code>     let a = self.items[index]
       a![result]! = itemDetails as [String:String]
  </code></pre>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <pre><code>    let valueDict = (value as! NSDictionary) as Dictionary
  
      for (key, val) in valueDict {
          let keyString = key as! String
          let valString = val as! String
  
          self.items[index]![result]![keyString]! = valString
      }
  </code></pre>
  
  <p>But it's throwing error:</p>
  
  <blockquote>
    <p>fatal error: unexpectedly found nil while unwrapping an Optional value</p>
  </blockquote>
  
  <p>But Surprisingly debugging shows all values:</p>
  
  <pre><code>po index : 1
  po itemId : "123123"
  po keyString: "keyInString"
  po valString: "valInString"
  </code></pre>
  
  <hr>
  
  <p><strong>Update 3</strong>:</p>
  
  <pre><code> for index in 1...5 {
     var results = [String]()
  
     // First I retrieve nearby users and assign it to key
  
     let itemsRef = Firebase(url: self.secret + "/items")
     eventsRef.queryOrderedByChild("user_id").queryEqualToValue(key).observeEventType(.ChildAdded, withBlock: { snapshot in
     // ^ above 'key' is the user_id retrieved before
  
     let itemDetails = snapshot.value // item details - [key:val, key:val]
     let itemId = snapshot.key        // item ids [id1,id2,id3]
  
     // I used 'KeyAndDetails' to store all values with ids 
     let IdsAndDetails = [itemId: itemDetails]
  
     self.itemIdsArray = []
     self.itemIdsArray.append(itemId)
  
     if index == 1 {
         // self.items = [
         //    index : [itemId : itemDetails]
         // ]
         // ^ This worked and gave me the structure
         // but I don't want to overwrite it, instead, I want to append
         // on the dictionary
  
         // This is where I am trying to append into `self.items`,
         // and throws error:
         self.items[index]?[result] = (eventDetails as! [String : String])
     }
    ...
  </code></pre>