Xcode - 连接大字符串时 EXEC_BAD_ACCESS

发布于 2024-08-31 15:02:09 字数 424 浏览 6 评论 0原文

连接大字符串时,我收到 EXEC_BAD_ACCESS。

我从提要中读取并创建我的网络视图,我构建了我的字符串,如下所示:

NSString *pageData = @"<h1>header</h1>";

pageData = [pageData stringByAppendingFormat@"<p>"];
pageData = [pageData stringByAppendingFormat@"self.bodyText"];
pageData = [pageData stringByAppendingFormat@"</p>"];
etc

我遇到的问题是 self.bodytext 是 21,089 个字符,其中有空格,当我对单词进行计数时。 有更好的方法吗?

谢谢

I'm getting a EXEC_BAD_ACCESS when concatenting a large string.

I've read from a feed and to create my webview I build up my string like:

NSString *pageData = @"<h1>header</h1>";

pageData = [pageData stringByAppendingFormat@"<p>"];
pageData = [pageData stringByAppendingFormat@"self.bodyText"];
pageData = [pageData stringByAppendingFormat@"</p>"];
etc

The problem I've got is self.bodytext is 21,089 characters with spaces when I do a count on word.
Is there a better method for doing this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

薄荷港 2024-09-07 15:02:10

我怀疑字符串的长度确实是一个问题。 50,000 个字符的字符串仅约 100 KB。但您在使用格式字符串时要非常小心。如果你的字符串包含看起来像格式说明符的东西,最好有一个相应的参数,否则如果你幸运的话你会得到垃圾,如果你不幸运的话你会崩溃。我怀疑这是错误,因为从您的描述中没有其他明显的问题。请小心您放入其中的内容,并避免将动态文本放入格式字符串中 - 只需在格式字符串中放入 %@ 并将动态文本作为参数传递即可。

I doubt the length of the string is really a problem. A 50,000-character string is only about 100 KB. But you want to be very careful about using format strings. If your string contains something that looks like a formatting specifier, there had better be a corresponding argument or you'll get garbage if you're lucky and a crash if you're not. I suspect this is the error, since there is no other obvious problem from your description. Be careful about what you put in there, and avoid ever putting dynamic text in a format string — just put a %@ in the format string and pass the dynamic text as an argument.

软的没边 2024-09-07 15:02:10

处理任意字符串时,使用appendString:而不是appendFormat:。

pageData = [pageData stringByAppendingString:@"<p>"];
pageData = [pageData stringByAppendingString:@"self.bodyText"];
pageData = [pageData stringByAppendingString:@"</p>"];

或者不要使用任意字符串作为格式:

pageData = [pageData stringByAppendingFormat:@"<p>%@</p>" , @"self.bodyText"];

如果要分段构建字符串,请使用 NSMutableString 而不是多个 stringBy 调用。

请记住,% 是格式化字符串和 url 转义的特殊字符,因此如果 bodyText 包含 url,则很容易导致崩溃。

Use appendString: instead of appendFormat: when dealing with arbitrary strings.

pageData = [pageData stringByAppendingString:@"<p>"];
pageData = [pageData stringByAppendingString:@"self.bodyText"];
pageData = [pageData stringByAppendingString:@"</p>"];

or do not use an arbitrary string as the format:

pageData = [pageData stringByAppendingFormat:@"<p>%@</p>" , @"self.bodyText"];

If you are building the string up in pieces, use NSMutableString instead of several stringBy calls.

Remember that % is a special character for formatted strings and for url escapes, so if bodyText contains a url it could easily cause a crash.

行至春深 2024-09-07 15:02:09

您肯定会想使用 NSMutableString 对于这样的东西:

NSMutableString * pageData = [NSMutableString stringWithCapacity:0];

[pageData appendFormat:@"<h1>header</h1>"];
[pageData appendFormat:@"<p>"];
...

NSMutableString 是为这种顺序连接而设计的,其中基本 NSString 类实际上并不意味着以这种方式使用。您的原始代码实际上会在每次调用 stringByAppendFormat: 时分配一个新的 NSString,然后继续将您已附加的所有数千个字符复制到其中。这很容易导致内存不足错误,因为随着您添加越来越多的调用,临时字符串的大小将呈指数级增长。

当您调用appendFormat:时,使用NSMutableString不会重新复制所有字符串数据,因为可变字符串维护一个内部缓冲区,并且只是将新字符串添加到其末尾。根据字符串的大小,您可能需要提前保留大量内存(对 ...WithCapacity: 参数使用有意义的数字)。但除非您确实遇到性能问题,否则没有必要走这条路。

You would definitely want to use NSMutableString for something like this:

NSMutableString * pageData = [NSMutableString stringWithCapacity:0];

[pageData appendFormat:@"<h1>header</h1>"];
[pageData appendFormat:@"<p>"];
...

NSMutableString is designed for this kind of sequential concatenation, where the basic NSString class is really not meant to be used in this manner. Your original code would actually allocate a new NSString every time you called stringByAppendFormat:, and then procede to copy into it all of the thousands of characters you had already appended. This could easily result in an out of memory error, since the size of the temporary strings would be growing exponentially as you add more and more calls.

Using NSMutableString will not re-copy all of the string data when you call appendFormat:, since the mutable string maintains an internal buffer and simply tacks new strings on to the end of it. Depending on the size of your string, you may want to reserve a huge chunk of memory ahead of time (use a meaningful number for the ...WithCapacity: argument). But there is no need to go that route unless you actually run into performance issues.

苍暮颜 2024-09-07 15:02:09

您的示例代码存在一些问题:

  1. 您应该使用 NSMutableString 通过附加多个部分来构建输出字符串。 NSString 是一个不可变的类,这意味着每次调用 stringByAppendingFormat: 时,您都会产生创建额外的新 NSString 对象的开销,该对象需要由自动释放池收集和释放。

    NSMutableString * pageData = [NSMutableString stringWithCapacity:0];

  2. 您应该在 NSMutableString 上使用 appendString: 来附加内容,而不是 <代码>stringByAppendingFormat:或appendFormat:。格式方法旨在基于格式说明符创建新字符串,其中包括作为占位符的特殊字段。请参阅 格式化字符串对象了解更多详细信息。当您将 stringByAppendingFormat: 与代码中的文字字符串一起使用时,您会产生解析字符串以查找不存在的占位符的开销,更重要的是,如果字符串碰巧有如果其中包含占位符(或类似的东西),您最终会遇到 EXEC_BAD_ACCESS 崩溃。当您附加 bodyText 时,很可能会发生这种情况。因此,如果您只想将 '

    ' 附加到您的 NSMutableString 中,请执行以下操作:

    [pageDataappendString:@"

    "];

  3. 如果您想要将 self.bodyText 属性的内容附加到字符串中,则不应将属性名称放在字符串文字中(即 @"self.bodyText" 是文字字符串 " self.bodyText”,而不是属性的内容。尝试:

    [pageDataappendString:self.bodyText];

例如,您实际上可以使用格式规范来组合示例代码的所有三行:

pageData = [pageData stringByAppendingFormat:@"<p>%@</p>", self.bodyText];

在格式规范中 %@ 是一个占位符,表示插入发送的结果对于 NSString 来说,这只是字符串的内容。

There are a few problems with your sample code:

  1. You should be using a NSMutableString to build up an output string by appending multiple parts. NSString is an immutable class which means that each time you call stringByAppendingFormat: you are incurring the overhead of creating an additional new NSString object which will need to be collected and released by the autorelease pool.

    NSMutableString * pageData = [NSMutableString stringWithCapacity:0];

  2. You should use appendString: on your NSMutableString to append content, instead of stringByAppendingFormat: or appendFormat:. The format methods are intended for creating new strings based on a format specifier which includes special fields as placeholders. See Formatting String Objects for more details. When you're using stringByAppendingFormat: with just a literal string like your code has, you are incurring the overhead of parsing the string for the non-existant placeholders, and more importantly, if the string happens to have a placeholder (or something that looks like one) in it, you'll end up with the EXEC_BAD_ACCESS crash that you are getting. Most likely this happening when your bodyText is appended. Thus if you simply want to append a '

    ' to your NSMutableString do something like this:

    [pageData appendString:@"<p>"];

  3. If you want to append the contents of the self.bodyText property to the string, you shouldn't put the name of the property inside of a string literal (i.e. @"self.bodyText" is the literal string "self.bodyText", not the contents of the property. Try:

    [pageData appendString:self.bodyText];

As an example, you could actually combine all three lines of your sample code by using a format specification:

pageData = [pageData stringByAppendingFormat:@"<p>%@</p>", self.bodyText];

In the format specification %@ is a placeholder that means insert the result of sending the description or descriptionWithLocale: message to the object. For an NSString this is simply the contents of the string.

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