创建很长的 NSString 会导致内存问题

发布于 2024-10-27 12:20:38 字数 1458 浏览 3 评论 0原文

下面的代码导致我的应用程序退出,即黑屏,然后在调试器控制台中看到:程序收到信号:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-11-03 12:20:38

问题是在每次迭代中都会形成一个新的字符串对象。这会消耗大量内存。一种解决方案可能是使用本地自动释放池,但这在这里相当复杂。

您应该使用 NSMutableString,例如:

NSMutableString *outStr = [[[NSMutableString alloc] init] autorelease];
for (int i = 0; i < count; i++) {
    NSDictionary *dict = [[ARAppDelegate sharedAppDelegate].orderArray objectAtIndex:i];
    [outStr appendFormat:@"%@,%@,%@,%@\n", 
          [dict valueForKey:@"CODE"], 
          [dict valueForKey:@"QTY"],
          [[ARAppDelegate sharedAppDelegate].descDict valueForKey:[dict valueForKey:@"CODE"]],
          [[ARAppDelegate sharedAppDelegate].priceDict valueForKey:[dict valueForKey:@"CODE"]]];
}

然后您可以使用 outStr,就像它是 NSString 一样。正如 Tom 在评论中指出的那样,您可以在完成后将 NSMutableString 转换为 NSString,使用:

NSString *result = [NSString stringWithString:outStr];

[outStr release]; // <-- add this line and remove the autorelease
                  //     from the outStr alloc/init line

使您的代码可重用且更易于维护。

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:

NSMutableString *outStr = [[[NSMutableString alloc] init] autorelease];
for (int i = 0; i < count; i++) {
    NSDictionary *dict = [[ARAppDelegate sharedAppDelegate].orderArray objectAtIndex:i];
    [outStr appendFormat:@"%@,%@,%@,%@\n", 
          [dict valueForKey:@"CODE"], 
          [dict valueForKey:@"QTY"],
          [[ARAppDelegate sharedAppDelegate].descDict valueForKey:[dict valueForKey:@"CODE"]],
          [[ARAppDelegate sharedAppDelegate].priceDict valueForKey:[dict valueForKey:@"CODE"]]];
}

Then you can use outStr, just as if it was an NSString. As Tom points out in the comments, you could turn the NSMutableString into an NSString when you're finished, using:

NSString *result = [NSString stringWithString:outStr];

[outStr release]; // <-- add this line and remove the autorelease
                  //     from the outStr alloc/init line

making your code re-usable and easier to maintain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文