我有这段代码,从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];
在iOS 5.1和5.1.1中不会崩溃