2014-01-17 36 views
0

我有單例類,有一個可重用的字符串操作代碼,有時會泄漏內存。當傳遞給方法並使用返回的Nsstring對象時,NSString操作(內存管理)的正確方式

-(NSString *)getLocalizedString:(NSString *)key 
{ 
@autoreleasepool 
{ 
    FMDatabase *db=[FMDatabase databaseWithPath:databasePath]; 
    [db open]; 
//  NSString *uppercaseValue=[key uppercaseString]; 
    NSString *uppercaseValue = [[NSString alloc] initWithString:[key uppercaseString]]; 
    NSString *slashRemoved=[uppercaseValue stringByReplacingOccurrencesOfString:@"/" withString:@" "]; 
    NSString *spaceRemoved=[slashRemoved stringByReplacingOccurrencesOfString:@" " withString:@"_"]; 
    NSString *dotRemoved=[spaceRemoved stringByReplacingOccurrencesOfString:@"." withString:@""]; 
    NSString *collanRemoved=[dotRemoved stringByReplacingOccurrencesOfString:@":" withString:@""]; 
    NSString *excalmaRemoved=[collanRemoved stringByReplacingOccurrencesOfString:@"!" withString:@""]; 
    NSString *commaRemoved=[excalmaRemoved stringByReplacingOccurrencesOfString:@"," withString:@""]; 
    NSString *webaddressRemoved=[commaRemoved stringByReplacingOccurrencesOfString:@"ABCCOM" withString:@"ABC.COM"]; 
    NSString *value=[db stringForQuery:@"select value from language where name=? AND key=?",[Utils getCurrentLanguage],webaddressRemoved]; 
    if(value==nil) 
    { 
     value=key; 
    } 
    return value; 

} 

}

用法:

NSString *textString = [[NSString alloc]initWithString:[langmodule getLocalizedString:[[arrSetting objectAtIndex:indexPath.row] objectForKey:@"name"]]]; 
    [cell.textLabel setText:textString]; 

[tip setTitle:[langmodule getLocalizedString:[[self.transactionSource objectAtIndex:indexPath.row] valueForKey:@"tip"]] forState:UIControlStateNormal]; 

* 方法getLocalizedString有時泄漏在 的NSString 值= [DB stringForQuery:@「從語言選擇值,其中name = AND key =?「,[Utils getCurrentLanguage],webaddressRemoved];

+getCurrentLanguage方法是這樣的:

+(NSString *)getCurrentLanguage 
{ 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; 
NSString *currentLanguage = [languages objectAtIndex:0]; 
if([currentLanguage isEqualToString:@"fr"]) 
{ 
    [email protected]"french"; 
} 
else if([currentLanguage isEqualToString:@"de"]) 
{ 
    [email protected]"german"; 
} 
else if([currentLanguage isEqualToString:@"ja"]) 
{ 
    [email protected]"japanese"; 
} 
else if([currentLanguage isEqualToString:@"es"]) 
{ 
    [email protected]"spanish"; 
} 
else if([currentLanguage isEqualToString:@"nl"]) 
{ 
    [email protected]"dutch"; 
} 
else if([currentLanguage isEqualToString:@"pt"]) 
{ 
    [email protected]"portuguese"; 
} 
else if([currentLanguage isEqualToString:@"zh-Hans"]) 
{ 
    [email protected]"chinese"; 
} 
else if([currentLanguage isEqualToString:@"ko"]) 
{ 
    [email protected]"korean"; 
} 
else if([currentLanguage isEqualToString:@"it"]) 
{ 
    [email protected]"italian"; 
} 
else { 
    [email protected]"english"; 
} 

return currentLanguage; 

}

是我getLocaliztionString罰款,或者是有內存問題。該項目在ARC上。 感謝&問候

+0

也許它具有泄漏FMDatabase?你有檢查嗎? – Joshua

回答

0

的管理定位在iphone的最好方式,我發現,使用TSLanguageManager庫,你可以從下面的鏈接檢查:https://github.com/tonisalae/TSLanguageManager

+0

感謝您的關注。我正在尋找解決方案而不是選項,我們不能因爲當前項目中有很多依賴關係,將會看上去並在新項目中使用。 – Alok

相關問題