我有这段代码从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];
答案 0 :(得分:2)
这在iOS 5.1或5.1.1中不会崩溃