views:

237

answers:

1

I'm trying to get sorted localized data from a core data model. My code:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName: entityDescriptionValue
                                                     inManagedObjectContext: context];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: entityDescription];

[fetchRequest setPredicate: predicate];

NSSortDescriptor *sortBy = [[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES selector: @selector(localizedCompare:)];
[fetchRequest setSortDescriptors: [NSArray arrayWithObject: sortBy]];
[sortBy release];

But, when I get the objects, they still come sorted with the original values (non-localized data).

What am I not doing right?

A: 

How do you get the localized names from the CoreData entity? It's not that localizedCompare: magically retrieves the localized version of strings and compare them. Instead, it uses the localized sort orders to the same strings. The sorted version of an array containing @"England", @"France", @"Germany" would be the same in any country... they don't contain any accented characters or whatever. The result of localizedCompare: can only change when two languages use different sort order among letters.

If you want to retrieve something from Localizable.strings based on the property of the CoreData entities, you need to do that manually. Sqlite backend of CoreData won't handle that at the level of fetch requests. So you need to first fetch the entries in an NSArray, and then need to sort them using sortedArrayUsing...: by passing a method which retrieves the localized names by NSLocalizedString and compares them.

Yuji
Hmm, thanks. I had already done that, but I thought that Core Data would do that for me somehow.Anyway, problem solved, thanks.
Tiago