Monday, May 23, 2011

NSLocale initWithLocaleIdentifier example objc

NSLocale initWithLocaleIdentifier mehod takes a string specifying locale and returns a NSLocale object. The NSLocale object is used to specify locale options in various other methods such as NSDateFormatter:dateFormatFromTemplate:options:locale: and so on.
[NSLocale initWithLocaleIdentifier example]

[Example code quoted from Apple Developer Site].

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *usFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:usLocale];
NSLog(@"usFormatterString: %@", usFormatString);

// Output: usFormatterString: EEE, MMM d

NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:gbLocale];
NSLog(@"gbFormatterString: %@", gbFormatString);
// Output: gbFormatterString: EEE d MMM