NSNumericSearch Solution for iOS

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.

+4
source share
4 answers

I finally found the answer to the question about sorting strings numerically. This involves using the block directly. For my sort descriptor, this is simple:

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

This allows you to sort NSStrings numerically (1, 11, 2, 25).

I am not sure why the category is not working. Thought I'd send an answer to others who run into the same problem.

+8
source

apparently only compare :, caseInsensitiveCompare :, localizedCompare: and localizedCaseInsensitiveCompare: work as sorting selectors when fetching in iOS

Really? I think the problem is that the selector you are using can only take one parameter (another line for comparison), and you need to use -compare:options: for numerical comparison.

Try writing a category method for comparison, for example.

 @interface NSString(NumericCompare) -(NSComparisionResult) compareNumerically: (NSString*) anotherString; @end @implementation NSString(NumericCompare) -(NSComparisionResult) compareNumerically: (NSString*) anotherString { return [self compare: anotherString options: NSNumericSearch]; } 

I tried pouring a string in NSNumber. An error has occurred with ARC since NSNumber is basically an int.

You cannot just point the NSSstring to NSNumber, you are just throwing a pointer, not the object that it points to. You can do it

 NSNumber* number = [NSDecimalNumber numberWithString: myStringValue]; 

NSDecimalNumber is a subclass of NSNumber .

0
source

If there really is a limitation (in fact, I only use localizedCaseInsensitiveCompare: in my code), you should be able to use sortDescriptorWithKey:ascending:comparator: and use NSNumericSearch in your block.

0
source

I managed to get what I need without NSNumericSearch.

I changed the structure of the master data by changing the 'size' attribute to Integer16 and changing the class property of the 'size' class from NSString to NSNumber and made an easy migration.

Then I modified my existing code a bit using:

 aString = [aNumber stringValue]; 

and

 NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init]; numformatter.numberStyle = NSNumberFormatterNoStyle; bNumber = [numformatter numberFromString:bString] 

where necessary, to modify the NSString and NSNumber objects.

Now it looks like what I want.

In conclusion, I avoided the whole problem of numerically sorting NSStrings by changing the data structure.

Sort descriptors were only the main ones used in fetchedResultsController:

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey1 ascending:YES]; NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor,sortDescriptor2, nil]; 

Thank you all for your input. Sorry for the trouble.

0
source

Source: https://habr.com/ru/post/1388611/


All Articles