Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用nspredicate对非英语字符串进行排序?_Objective C_Ios_Cocoa Touch_Core Data_Nssortdescriptor - Fatal编程技术网

Objective c 如何使用nspredicate对非英语字符串进行排序?

Objective c 如何使用nspredicate对非英语字符串进行排序?,objective-c,ios,cocoa-touch,core-data,nssortdescriptor,Objective C,Ios,Cocoa Touch,Core Data,Nssortdescriptor,我正在使用排序描述符对提取请求的结果进行排序 NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:[MyEntity entityName]]; NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"property"

我正在使用排序描述符对提取请求的结果进行排序

NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:[MyEntity entityName]];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"property" 
                                                           ascending:YES 
                                                            selector:@selector(localizedCompare:)];
req.sortDescriptors = [NSArray arrayWithObject:descriptor];
return [self.managedObjectContext executeFetchRequest:req error:nil];
问题在于,以非英语字符(如“İ”)开头的单词列在列表的末尾。这是一个土耳其语字母,字母表如下所示:

A、 B,C,Ğ,D,E,F,G,Ğ,H,I,İ,J,K,L,M,N,O,Ö,p,R,S,Ş,T,U,Ü,V,Y,Z

所以字母在第12位

我不知道为什么,但在获取对象后使用comparator是有效的。因此,它适用于任何数组,但不适用于提取请求的排序描述符。

试试看

[NSSortDescriptor alloc] initWithKey:@"property" ascending:YES selector:@selector(localizedCompare:)]
编辑

@默特更新了他的问题。现在看来,
localizedCompare:
对土耳其语字母进行了正确排序,但无法处理fetch请求

下面是我为测试这个问题所做的工作。也许您可以检查这是否在您的环境中起作用,然后从那里开始工作:

// Create some entities:
NSArray *a = @[@"İ", @"J", @"Ğ", @"G", @"H", @"I", @"Ç", @"C"];
for (NSString *s in a) {
    MyEntity *e = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity"
                                                inManagedObjectContext:self.managedObjectContext];
    e.name = s;
}

// Fetch all entities in sorted order:
NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                           ascending:YES
                                                            selector:@selector(localizedCompare:)];
req.sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *result = [self.managedObjectContext executeFetchRequest:req error:nil];

“MyEntity”是一个核心数据实体,具有一个字符串类型的属性“name”。

请查看我在上一节中问题的详细信息,我将演示如何使用UILocalizedIndexedCollation正确生成字母表,并使用基于UILocalizedIndexCollation的正确排序方法进行排序。我的问题只是围绕着要求一个更好的方法来做这件事


如果不使用UILocalizedIndexCollation,则应仅使用本地化标准比较:而不是WWDC视频中提到的本地化比较。

我在以下方面也有类似问题:

[strArr-SortedArrayingSelector:@selector(localizedCompare:)]

看来

本地化比较

召唤

比较:其他选项:nil范围:NSMakeRange(0,self.length)区域设置:[NSLocale currentLocale]]

[NSLocale currentLocale]
可能处于混合状态,具体取决于用户的首选项。因此,需要基于用户语言创建一个干净的
NSLocale

尝试在
NSString
上创建包含以下内容的类别:

-(NSComparisonResult)currentLocalCompare:(id)other
{
    NSLocale * currLoc = [NSLocale currentLocale]; //get current locale
    NSLocale * loc = [[NSLocale alloc] initWithLocaleIdentifier:currLoc.localeIdentifier];//lets create a new clean NSLocale based on users prefared langauge
    return [self compare:other options:nil range:NSMakeRange(0, self.length) locale:loc];//compare using clean NSLocale
}
把它叫做:


[strArr sortedArrayUsingSelector:@selector(currentLocalCompare:)]

它不起作用。我还试着改变模拟器的语言,这很奇怪。如果使用
[myArray SortedArray UsingSelector:@selector(localizedCompare:)]
对数组进行排序,则得到正确的结果。你能展示你如何设置获取请求的代码吗?我用几乎相同的方法(使用一个获取结果控制器)完成了它,它适用于德语umlauts和其他国际字符。顺便说一句:你是否使用ARC编译?我已经试过了,但会再次问你在项目设置中的本地化本地开发区域是什么?是的,我正在使用Arc进行编译localizedCompare函数从何处获得语言?