Question

The following code gives an array with all Languages, still I would rather filter out variations like "en-GB",""zh-Hant" and have an array with only 2 characters per language.

What is the elegant Apple(ish) way to do so?

 NSLog(@"[NSLocale preferredLanguages] = %@",[NSLocale preferredLanguages]);
 [self.selectedArray  addObjectsFromArray:[NSLocale preferredLanguages]];
Was it helpful?

Solution

Using predicates to filter arrays is probably as "Appleish" as it gets:

NSPredicate *lenIs2=[NSPredicate predicateWithFormat:@"length==2"];
NSArray *filtered = [[NSLocale preferredLanguages] filteredArrayUsingPredicate:lenIs2];
NSLog(@"%@", filtered);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top