开发者

Creating a NSSortDescriptor using a given locale

开发者 https://www.devze.com 2023-03-16 13:30 出处:网络
I\'m using a NSFetchRequest to access a persistent store, into which I pass a NSSortDescriptor. The content to be sorted is in Norwegian, so I want it to always sort using the \"nb-NO\" locale, no mat

I'm using a NSFetchRequest to access a persistent store, into which I pass a NSSortDescriptor. The content to be sorted is in Norwegian, so I want it to always sort using the "nb-NO" locale, no matter the user preferences.

Right now I'm using the system locale to determine the sorting:

NSSortDescriptor *sorting = [NSSortDescriptor sortDescriptorWithKey:@"titl开发者_运维问答e" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

What's the simplest/cleanest way to do this?


Select your project--> info ---> Set "Localization native development region" to "nb-NO". Dont forget to add the specific Localisaation files to your project. :)

If you want to sort according to your device language then below is the code

// Using fetch request 
-(NSFetchRequest*)returnFetchRequest:(NSFetchRequest*)fetchRequest
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Your Entity Name" inManagedObjectContext:YOUR CONTEXT];
    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:[NSString stringWithFormat:@"%@",NSLocalizedString(@"title", nil)] ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    return fetchRequest;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消