title: Save Recent Searches in NSUserDefaults
body: <p>I have UIViewController having a UISearchBar and UITableView. The table view is used to show recent searches.</p>
  
  <p>I am using NSUserDefaults to save the search texts.</p>
  
  <p>Here is what I'm doing:</p>
  
  <pre><code>- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  {
      DLog(@"Search")
      NSString *searchText = [self.searchBar text];
      [self.recentSearchesDict setObject:searchText forKey:searchText];
  
      self.recentSearchesStrings = [[self.recentSearchesDict allValues] mutableCopy];
  
      [self saveRecentSearches];
  
      [self.recentSearchList reloadData];
  }
  </code></pre>
  
  <p>I save the text object in NSMutableDictionary with the same text to as the key to avoid duplicate entries in the table view.</p>
  
  <p>and:</p>
  
  <pre><code>- (void)saveRecentSearches
  {
      NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
  
      if (self.recentSearchesDict) {
          [currentDefaults setValue:self.recentSearchesDict forKey:@"recentSearchesArray"];
      }
  
  }
  </code></pre>
  
  <p>saves the NSMutableDictionary to NSUserDefaults.</p>
  
  <p>In viewDidLoad I'm calling this:</p>
  
  <pre><code>- (void)loadRecentSearches
  {
      NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
      self.recentSearchesDict = [[currentDefaults objectForKey:@"recentSearchesArray"] mutableCopy];
  
      if (!self.recentSearchesDict) {
          self.recentSearchesStrings = [[NSMutableArray alloc] init];
          self.recentSearchesDict = [NSMutableDictionary dictionary];
  
          return;
      }
  
      self.recentSearchesStrings = [[self.recentSearchesDict allValues] mutableCopy];
  }
  </code></pre>
  
  <p>for populating the table view I'm using the NSMutableArray object recentSearchesStrings.</p>
  
  <p>The problem is that when the array takes all values from the dictionary, they are not in order of most recent.</p>
  
  <p>I want the latest to be displayed on top of the list.</p>
  
  <p>Any help? Is there any other better approach to achieve the desired task?</p>