-1
+ (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相關的變化

+0

當問題代碼可以在iOS版本中運行而不在其他版本中時,這很正常。因爲Apple每次發佈說明都會更改系統。所以只有沒有問題的代碼才能在所有的iOS中工作 – larva

回答

0

在你的代碼只有一個頁頭辭典

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; 

所以你只需要關心你通過它,另一個目的是不歸!所以你不需要發佈或autorelease他們。

嘗試流動代碼

+ (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]; 
      //NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease]; 
      //NSString *keyValue = [[[NSString alloc]init]autorelease]; 
      NSDictionary *resourceBundle = [dict valueForKey:@"hash"]; 
      // relese dict here because not use after 
      [dict release]; 
      NSString *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]; 
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
        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]; 
       NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
       NSLog(@"value for %@ is : %@",i18nkey,[dict objectForKey:i18nkey]); 
       return [dict objectForKey:i18nkey]; 
      } 
      else 
      { 
       return NULL; 
      } 
     } 
    } 
0

在手動引用計數,保留和釋放需要平衡。

NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:filePath]autorelease]; 
NSDictionary *resourceBundle = [[[NSDictionary alloc] init]autorelease]; 

的保留和釋放是平衡的,因爲alloc(連同retainnewcopymutableCopy)返回一個保留實例,autorelease計數作爲release

然而,在

NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]autorelease]; 

你有一個overrelease因爲你是autorelease東西,你都沒有保留。

iOS版本與它完全無關。

相關問題