适用于iOS的NSNumericSearch解决方案 [英] NSNumericSearch solution for iOS

查看:151
本文介绍了适用于iOS的NSNumericSearch解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSString,我想用数字搜索。似乎NSNumericSearch将是合适的解决方案,但显然只比较:,caseInsensitiveCompare:,localizedCompare:和localizedCaseInsensitiveCompare:在iOS中进行提取时用作排序选择器。 (这是一个核心数据项目)。据我所知,这与SQLite后备数据存储有关。

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.

我已经尝试将字符串转换为NSNumber。 ARC有一个错误,因为NSNumber基本上是一个int。

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

总之,我想用数字(1,2,11,25)对字符串进行排序为反对(1,11,2,25)。我很感激任何解决方案。

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.

(我为上述情况道歉。当时我远离我的电脑,匆忙。)

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

有限排序描述符的信息来自Isted& amp;的最后一页Harrington 2011年出版的iOS版核心数据。

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


使用排序描述符进行获取时,您需要限制自己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

fetchedResultsCntroller中的排序描述符如下:

The sort descriptor in fetchedResultsCntroller is as follows:

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

它返回此错误:

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'不支持的NSSortDescriptor选择器:sizeCompare:'

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

这似乎与Isted& amp ;哈灵顿的陈述。

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

另外,当我说我投入NSNumber时,我试图简短,但显然不清楚。这就是我做的:

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];

所以,我想用数字(1,2,11,25)对NSString进行排序到(1,11,2,25)。我仍然会感激任何解决方案。

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.

推荐答案

我终于找到了数字排序字符串问题的答案。它涉及直接使用块。对于我的排序描述符,它只是:

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]; }];

这允许NSStrings以数字方式排序(1,11,2,25)。

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.

这篇关于适用于iOS的NSNumericSearch解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆