title: NSNumericSearch solution for iOS
body: <p>I have an NSString that I would like to search numerically.  It seems that NSNumericSearch would be the appropriate solution, but apparently only compare:, caseInsensitiveCompare:, localizedCompare: and localizedCaseInsensitiveCompare: work as sort selectors when doing a fetch in iOS. (It's a core data project).  It is my understanding that this has something to do with the SQLite backing data store.</p>
  
  <p>I have tried casting the string to an NSNumber.  There is an error with ARC, as NSNumber is basically an int.</p>
  
  <p>In summary, I would like to sort a string numerically (1, 2, 11, 25) as opposed to (1, 11, 2, 25). I would appreciate any solutions.</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <p>EDIT: (I apologize for the above. I was away from my computer at the time, and in a hurry.)</p>
  
  <p>The information about the limited sort descriptors comes from the last page of Isted &amp; Harrington's 2011 book 'Core Data for iOS'.</p>
  
  <blockquote>
    <p><em>When doing a fetch with sort descriptors you'll need to restrict yourself to the following selectors on iOS:
    compare:,
    caseInsensitiveCompare:,
    localizedCompare:,
    localizedCaseInsensitiveCompare:</em>.</p>
  </blockquote>
  
  <p>For clarification, I wrote a category like this:</p>
  
  <pre><code>@interface NSString (SizeComparison)
  -(NSComparisonResult)sizeCompare:(NSString*)otherString;
  @end
  @implementation NSString (SizeComparison)
  -(NSComparisonResult)sizeCompare:(NSString*)otherString {
  return [self compare:otherString options:NSNumericSearch];
  }
  @end
  </code></pre>
  
  <p>The sort descriptor in fetchedResultsCntroller is as follows:</p>
  
  <pre><code>NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES selector:@selector(sizeCompare:)];
  </code></pre>
  
  <p>It returns this error:</p>
  
  <p><em>*</em> Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: sizeCompare:'</p>
  
  <p>This seems to be consistent with Isted &amp; Harrington's statement.</p>
  
  <p>Also, when I said that I casted to NSNumber, I was trying to be brief, but obviously was unclear. This is what I did:</p>
  
  <pre><code>NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init];
  numformatter.numberStyle = NSNumberFormatterNoStyle;
  self.objectToEdit.size = [numformatter numberFromString:self.sizeTextField.text];
  </code></pre>
  
  <p>So, I would like to sort an NSString numerically (1, 2, 11, 25) as opposed to (1, 11, 2, 25). I would still appreciate any solutions.</p>