stringByAppendingFormat 不起作用

发布于 2024-09-01 02:40:16 字数 234 浏览 4 评论 0 原文

我有一个 NSString 并且无法应用以下语句:

NSString *myString = @"some text";
[myString stringByAppendingFormat:@"some text = %d", 3];

没有日志或错误,字符串只是没有更改。我已经尝试过 NSString (如文档所述)和 NSMutableString。

欢迎任何线索。

I have an NSString and fail to apply the following statement:

NSString *myString = @"some text";
[myString stringByAppendingFormat:@"some text = %d", 3];

no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.

any clues most welcome.

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

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

发布评论

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

评论(4

烟凡古楼 2024-09-08 02:40:16

我建议更正为 (文档):

NSString *myString = @"some text";
myString = [myString stringByAppendingFormat:@" = %d", 3];

来自文档:

返回一个字符串,该字符串是通过将给定格式字符串和以下参数构造的字符串附加到接收者而形成的。

I would suggest correcting to (documentation):

NSString *myString = @"some text";
myString = [myString stringByAppendingFormat:@" = %d", 3];

From the docs:

Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.

虐人心 2024-09-08 02:40:16

它正在工作,您只是忽略返回值,它是带有附加格式的字符串。 (参见文档。)您无法修改 NSString — 要修改 NSMutableString,请使用 -appendFormat: 代替。

当然,在您的玩具示例中,您可以将其缩短为:

NSString *myString = [NSString stringWithFormat:@"some text = %d", 3];

但是,您可能需要将格式字符串附加到其他地方创建的现有字符串中。在这种情况下,特别是如果您要附加多个部分,最好考虑并平衡使用可变字符串或多个不可变的自动释放字符串的利弊。

It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.

Of course, in your toy example, you could shorten it to this:

NSString *myString = [NSString stringWithFormat:@"some text = %d", 3];

However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.

苏佲洛 2024-09-08 02:40:16

使用 @"" 创建字符串始终会产生不可变的字符串。如果你想创建一个新的 NSMutableString ,请按以下步骤操作。

NSMutableString *myString = [NSMutableString stringWithString:@"some text"];
[myString appendFormat:@"some text = %d", 3];

Creating strings with @"" always results in immutable strings. If you want to create a new NSMutableString do it as following.

NSMutableString *myString = [NSMutableString stringWithString:@"some text"];
[myString appendFormat:@"some text = %d", 3];
放手` 2024-09-08 02:40:16

我在附加本地化字符串时收到类似的警告消息。我就是这样解决的

NSString *msgBody = [msgBody stringByAppendingFormat:@"%@",NSLocalizedString(@"LOCALSTRINGMSG",@"Message Body")];

I had a similar warning message while appending a localized string. This is how I resolved it

NSString *msgBody = [msgBody stringByAppendingFormat:@"%@",NSLocalizedString(@"LOCALSTRINGMSG",@"Message Body")];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文