title: "[NSBigMutableString substringWithRange:]: Range {0, 10} out of bounds; string length 9" error with Undo
body: <p>My app crashes when I try to do undo on <code>UISearchBar</code>. In my app I have code to prevent entering a '%' sign on the search bar and for doing this it replaces a % to @"" in textDidChange method as below:</p>
  
  <pre><code>- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  {
     self.searchBar.text = [searchText stringByReplacingOccurrencesOfString:@"%" withString:@""];
  }
  </code></pre>
  
  <p>So if I type text "abc%xyz", the final text visible on search bar would be 'abcxyz'. No when I click undo, I see 'xyz' gets cleared and I still see 'abc' on the search bar instead of clearing 'abcxyz' all at one go.   </p>
  
  <p>Now if I click undo again to clear 'abc' my app crashes with <code>[NSBigMutableString substringWithRange:]: Range out of bounds</code> error. </p>
  
  <p>I am assuming that even though '%' gets replaced by @"", the undo manager might still be holding it and hence the range is going out of bounds.</p>
  
  <p>I tried <code>[searchBar.undoManager removeAllActions];</code> in <code>textDidChange</code> after I replace % with @"", but it didn't help. Here is the code:</p>
  
  <pre><code>- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
      {
         self.searchBar.text = [searchText stringByReplacingOccurrencesOfString:@"%" withString:@""];
        [searchBar.undoManager removeAllActions];
      }
  </code></pre>
  
  <p><strong><em>Question:</em></strong> Anybody faced similar issue before? How do I handle the undo?</p>