我有單例類,有一個可重用的字符串操作代碼,有時會泄漏內存。當傳遞給方法並使用返回的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上。 感謝&問候
也許它具有泄漏FMDatabase?你有檢查嗎? – Joshua