创建很长的 NSString 会导致内存问题
下面的代码导致我的应用程序退出,即黑屏,然后在调试器控制台中看到:程序收到信号:“0”。
基本上,当我的 orderArray 计数为 2000 或更多时,它会导致问题。我使用的是装有 iOS 4.2 的 iPhone 3GS
问题:是否有更高效且占用内存更少的方法来创建我的 long outStr?
NSString *outStr = @"";
for (int i = 0; i < count; i++) {
NSDictionary *dict = [[ARAppDelegate sharedAppDelegate].orderArray objectAtIndex:i];
outStr = [outStr stringByAppendingFormat:@"%@,%@,%@,%@\n",
[dict valueForKey:@"CODE"],
[dict valueForKey:@"QTY"],
[[ARAppDelegate sharedAppDelegate].descDict valueForKey:[dict valueForKey:@"CODE"]],
[[ARAppDelegate sharedAppDelegate].priceDict valueForKey:[dict valueForKey:@"CODE"]]];
}
更新:感谢非常好心的人的帮助,下面是我修改后的代码:
NSArray *orderA = [ARAppDelegate sharedAppDelegate].orderArray;
NSDictionary *descD = [ARAppDelegate sharedAppDelegate].descDict;
NSDictionary *priceD = [ARAppDelegate sharedAppDelegate].priceDict;
NSMutableString *outStr = [[[NSMutableString alloc] init] autorelease];
for (int i = 0; i < [orderA count]; i++) {
NSDictionary *dict = [orderA objectAtIndex:i];
NSString *code = [dict valueForKey:@"CODE"];
[outStr appendFormat:@"%@,%@,%@,%@\n",
code,
[dict valueForKey:@"QTY"],
[descD valueForKey:code],
[priceD valueForKey:code]];
}
[self emailTxtFile:[NSString stringWithString:outStr]];
// 这到达了方法的结尾
My code below is causing my app to quit i.e. get black screen and then see in debugger console: Program received signal: “0”.
Basically it is causing problem when my orderArray has count of 2000 or more. I am using iPhone 3GS with iOS 4.2
Question: Is there a more efficient and less memory consuming way to create my long outStr?
NSString *outStr = @"";
for (int i = 0; i < count; i++) {
NSDictionary *dict = [[ARAppDelegate sharedAppDelegate].orderArray objectAtIndex:i];
outStr = [outStr stringByAppendingFormat:@"%@,%@,%@,%@\n",
[dict valueForKey:@"CODE"],
[dict valueForKey:@"QTY"],
[[ARAppDelegate sharedAppDelegate].descDict valueForKey:[dict valueForKey:@"CODE"]],
[[ARAppDelegate sharedAppDelegate].priceDict valueForKey:[dict valueForKey:@"CODE"]]];
}
Update: Thanks to very kind people who helped, below is my modified code:
NSArray *orderA = [ARAppDelegate sharedAppDelegate].orderArray;
NSDictionary *descD = [ARAppDelegate sharedAppDelegate].descDict;
NSDictionary *priceD = [ARAppDelegate sharedAppDelegate].priceDict;
NSMutableString *outStr = [[[NSMutableString alloc] init] autorelease];
for (int i = 0; i < [orderA count]; i++) {
NSDictionary *dict = [orderA objectAtIndex:i];
NSString *code = [dict valueForKey:@"CODE"];
[outStr appendFormat:@"%@,%@,%@,%@\n",
code,
[dict valueForKey:@"QTY"],
[descD valueForKey:code],
[priceD valueForKey:code]];
}
[self emailTxtFile:[NSString stringWithString:outStr]];
// This reaches end of method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是在每次迭代中都会形成一个新的字符串对象。这会消耗大量内存。一种解决方案可能是使用本地自动释放池,但这在这里相当复杂。
您应该使用
NSMutableString
,例如:然后您可以使用
outStr
,就像它是NSString
一样。正如 Tom 在评论中指出的那样,您可以在完成后将NSMutableString
转换为NSString
,使用:使您的代码可重用且更易于维护。
The problem is that in every iteration a new string object is formed. This consumes a lot of memory. One solution could be to use a local autoreleasepool, but that's rather complicated here.
You should use an
NSMutableString
, like:Then you can use
outStr
, just as if it was anNSString
. As Tom points out in the comments, you could turn theNSMutableString
into anNSString
when you're finished, using:making your code re-usable and easier to maintain.