我有这段代码可以从 NSLocale 获取一些信息(当前国家、国家名称)。
它在 iOS 4.3 中运行没有任何问题,但在 iOS 5 中崩溃。
经过检查,似乎 [locale localeIdentifier] 和 [locale displayNameForKey: value:] 根本不起作用,但在构建它时没有检测到警告和错误。
我该怎么做才能让它在 iOS 5 中运行?
// Create a pool for autoreleased objects
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Get the current country and locale information of the user
NSLocale *locale = [NSLocale currentLocale];
NSString *currentLocaleID = [locale localeIdentifier]; //returns en_US instead of the actual country stored in phone settings
NSDictionary *localeDictionary = [NSLocale componentsFromLocaleIdentifier:currentLocaleID];
NSString *currentCountry = [locale displayNameForKey:NSLocaleCountryCode
value:[localeDictionary objectForKey:NSLocaleCountryCode]]; // returns nil
// Get the list of country codes
NSArray *countryCodeArray = [NSLocale ISOCountryCodes];
NSMutableArray *sortedCountryNameArray = [NSMutableArray array];
for (NSString *countryCode in countryCodeArray) {
NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode]; //displayNameString is nil after executing this line
[sortedCountryNameArray addObject:displayNameString]; //app crashes here
}
// Drain the autoreleased pool
[pool drain];