I have an NSString that I would like to search numerically. It looks like NSNumericSearch would be a suitable solution, but apparently only compare :, caseInsensitiveCompare :, localizedCompare: and localizedCaseInsensitiveCompare: work as sorters when fetching in iOS. (This is the main data project). I understand that this has something to do with the SQLite data warehouse.
I tried pouring a string in NSNumber. An error has occurred with ARC since NSNumber is basically an int.
In conclusion, I would like to sort the string numerically (1, 2, 11, 25) as opposed to (1, 11, 2, 25). I would appreciate any solutions.
.
.
EDIT: (I apologize for the above. At that time I was out of the computer and in a hurry.)
For information about limited sorting descriptors, see the last page of Isted and Harrington 2011's Basic iOS Data.
When fetching with sorting descriptors, you need to limit yourself to the following selectors in iOS: compare :, caseInsensitiveCompare :, localizedCompare :, localizedCaseInsensitiveCompare :.
For clarification, I wrote this category:
@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 the fetchedResultsCntroller file is as follows:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES selector:@selector(sizeCompare:)];
It returns this error:
* Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "unsupported selector NSSortDescriptor: sizeCompare:"
This is similar to the statement of Easted and Harrington.
Also, when I said that I dropped NSNumber, I tried to be concise, but obviously not clear. 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 the NSString numerically (1, 2, 11, 25), not (1, 11, 2, 25). I would appreciate any solutions.