+ (NSString *)getValueforLocale:(NSString*) i18nkey :(NSString*)locale{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSLog(@"paths are : %@",paths);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"libraryDirectory : %@",libraryDirectory);
NSString *filePath = [libraryDirectory stringByAppendingPathComponent:@"I8nDB"];
filePath = [filePath stringByAppendingPathComponent:locale];
NSLog(@"file path is : %@",filePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if(fileExists)
{
NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:filePath]autorelease];
NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease];
NSString *keyValue = [[[NSString alloc]init]autorelease];
resourceBundle = [dict valueForKey:@"hash"];
keyValue=[resourceBundle valueForKey:i18nkey];
NSLog(@"value for %@ is(container) : %@",i18nkey,keyValue);
if(keyValue != nil || keyValue != NULL)
{
return keyValue;
}
else
{
NSLog(@"key not found in the container file");
NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
ofType:@"strings"
inDirectory:nil
forLocalization:locale];
NSLog(@"path for %@ is : %@",locale,path);
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExists)
{
NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
NSLog(@"value for %@ is(resources) : %@",i18nkey,[dict objectForKey:i18nkey]);
return [dict objectForKey:i18nkey];
}
else
{
return NULL;
}
}
}
else
{
NSLog(@"%@ locale does not exist in container",locale);
NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
ofType:@"strings"
inDirectory:nil
forLocalization:locale];
NSLog(@"path for %@ in resources is : %@",locale,path);
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExists)
{
NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease];
NSLog(@"value for %@ is : %@",i18nkey,[dict objectForKey:i18nkey]);
return [dict objectForKey:i18nkey];
}
else
{
return NULL;
}
}
}
如果我们从上面的代码中删除自动释放,它是工作在iOS7如果没有应用程序崩溃应用程序崩溃的iOS7但不是在8和9,由于NSDictionary中自动释放
我最关心的是,为什么它不在iOS8 & 9没有崩溃,只有在iOS7崩溃 是否有与这些版本的autorelease相关的变化
当问题代码可以在iOS版本中运行而不在其他版本中时,这很正常。因为Apple每次发布说明都会更改系统。所以只有没有问题的代码才能在所有的iOS中工作 – larva