gpt4 book ai didi

ios - 需要帮助修复内存泄漏 - NSMutableString

转载 作者:行者123 更新时间:2023-11-28 22:52:49 29 4
gpt4 key购买 nike

一直在玩弄仪器,但运气不太好,无法弄清楚如何解决此内存泄漏问题。

首先是代码:

-(NSString *) randomizeHint:(NSString *) wordToShuffle{

NSMutableString * outputstring = [NSMutableString stringWithCapacity:[wordToShuffle length]];
NSMutableSet * usedNumberSet = [NSMutableSet setWithCapacity:[wordToShuffle length]];

for (int i=0; i<[wordToShuffle length]; i++) {
int randomnum = arc4random()%[wordToShuffle length];

while ([usedNumberSet containsObject:[NSNumber numberWithInt:randomnum]]==YES) {
randomnum = arc4random()%[wordToShuffle length];
}

[usedNumberSet addObject:[NSNumber numberWithInt:randomnum]];
[outputstring appendFormat:@"%c",[wordToShuffle characterAtIndex:randomnum]];
}

CCLOG(@"outputstring is:%@",outputstring);
return outputstring;

}

Instruments 给我以下信息:

泄漏的对象 = NSCFString,负责的库 = 基础,负责的框架 = -[NSPlaceholderMutableString initWithCapacity:]

有什么想法吗?

提前致谢。

最佳答案

你并不真的需要使用可变字符串...特别是因为你的返回类型是 NSString。只需使用 stringByAppendingFormat:

-(NSString *) randomizeHint:(NSString *) wordToShuffle{

NSString * outputstring = @"";

NSMutableSet * usedNumberSet = [NSMutableSet setWithCapacity:[wordToShuffle length]];

for (int i=0; i<[wordToShuffle length]; i++) {
int randomnum = arc4random()%[wordToShuffle length];

while ([usedNumberSet containsObject:[NSNumber numberWithInt:randomnum]]==YES) {
randomnum = arc4random()%[wordToShuffle length];
}

[usedNumberSet addObject:[NSNumber numberWithInt:randomnum]];

// just set outputstring like so... no need to worry about a leaky mutable string then
outputstring = [outputstring stringByAppendingFormat:@"%c",
[wordToShuffle characterAtIndex:randomnum]];
}


return outputstring;

}

关于ios - 需要帮助修复内存泄漏 - NSMutableString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508699/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com