title: filtering a dictionary in swift
body: <p>I am trying to write a function which searches for a string in the dictionary and if found, keeps the entry, otherwise removes the entry from data source.</p>
  
  <p>This is my initial data source's structure </p>
  
  <pre><code>[String: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;]
  </code></pre>
  
  <p>Example data source:</p>
  
  <pre><code>[
    A: [
         [id: 1, name: "Android"], 
         [id: 22, name: "Apple"], 
         [id: 3, name: "Apricot"] 
    ],
    B: [
         [id: 33, name: "Bat"],
         [id: 45, name: "Breeze"]
    ]
  ]
  </code></pre>
  
  <p>Below is what my function looks like, here <code>ltrToCompare</code> is my search string,</p>
  
  <pre><code>func getFilteredData(data : [String: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;], ltrToCompare : String) -&gt; [String: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;] {
          // For keeping the filtered result
          var filteredData = [String: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;]()
  
          // Looping through parent array
          for (letter, arr) in data {
              // Filters the internal array, below code works when arr is an array containing strings ie., [String], doesn't work for Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;
              let filter = arr.filter() {
                  return $0.lowercaseString.rangeOfString(ltrToCompare.lowercaseString) != nil
              }
  
              // Checks whether the inner array filtering returns any element
              if (filter.count != 0) {
                  filteredData.append((letter, filter));
              }
          }
          return filteredData
      }
  </code></pre>
  
  <p>The above code works if my data structure is <code>[String: Array&lt;String&gt;]</code> instead of <code>[String: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt;]</code></p>
  
  <p>Any help is greatly appreciated. </p>