Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumericSearch solution for iOS

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.

I have tried casting the string to an NSNumber. There is an error with ARC, as NSNumber is basically an int.

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.

.

.

EDIT: (I apologize for the above. I was away from my computer at the time, and in a hurry.)

The information about the limited sort descriptors comes from the last page of Isted & Harrington's 2011 book 'Core Data for iOS'.

When doing a fetch with sort descriptors you'll need to restrict yourself to the following selectors on iOS: compare:, caseInsensitiveCompare:, localizedCompare:, localizedCaseInsensitiveCompare:.

For clarification, I wrote a category like this:

@interface NSString (SizeComparison)
-(NSComparisonResult)sizeCompare:(NSString*)otherString;
@end
@implementation NSString (SizeComparison)
-(NSComparisonResult)sizeCompare:(NSString*)otherString {
return [self compare:otherString options:NSNumericSearch];
}
@end

The sort descriptor in fetchedResultsCntroller is as follows:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES selector:@selector(sizeCompare:)];

It returns this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: sizeCompare:'

This seems to be consistent with Isted & Harrington's statement.

Also, when I said that I casted to NSNumber, I was trying to be brief, but obviously was unclear. This is what I did:

NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init];
numformatter.numberStyle = NSNumberFormatterNoStyle;
self.objectToEdit.size = [numformatter numberFromString:self.sizeTextField.text];

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.

like image 269
Kurt Avatar asked Feb 23 '23 00:02

Kurt


1 Answers

I finally found the answer to the problem of sorting strings numerically. It involves the use of a block directly. For my sort descriptor it is simply:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"size" ascending:NO comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }];

This allows the NSStrings to be sorted numerically (1, 11, 2, 25).

I'm not sure why a category didn't work. Thought I would post answer for others that encounter the same issue.

like image 195
Kurt Avatar answered Mar 03 '23 21:03

Kurt