\n 不会跳到 NSString 中的下一行

发布于 2024-10-26 19:08:28 字数 164 浏览 6 评论 0原文

NSMutableString *a = @"Hi";

NSMutableString *b =[a stringByAppendingString:@"\n\n Hi Again"];

上面的代码没有给出错误,但没有在下一行添加“Hi Again”。为什么?

NSMutableString *a = @"Hi";

NSMutableString *b =[a stringByAppendingString:@"\n\n Hi Again"];

The above doesn't give an error but does not put "Hi Again" on the next line. Why?

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

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

发布评论

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

评论(4

野稚 2024-11-02 19:08:28

编辑2
我在发帖后意识到,OP 的标题中有 NSString,但在代码中放入了 NSMutableString。我已提交编辑以将 NSMutableString 更改为 NSString。

我会留下这个,因为它仍然可能有帮助。


嗯,我很惊讶它没有给出错误,因为你给了一个 NSMutableString 一个 NSString。

您需要阅读 NSMutableStrings

给你一个主意

//non mutable strings
  NSString *shortGreetingString = @"Hi";
NSString *longGreetingString =  @"Hi Again";
 /*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/

NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];

/*The mutableString, now uses an appendFormat to construct the string
     each %@ in the Parameters for the appendFormat is a place holder for values of NSStrings
     listed in the order you want after the comma. 
    Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:@"%@\n\n%@",shortGreetingString,longGreetingString];


NSLog (@"mutableString = %@" ,mutableString);

[pool drain];

EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.

I will leave this as it still maybe helpful.


Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.

You need to read the Documentation on NSMutableStrings.

to give you an idea

//non mutable strings
  NSString *shortGreetingString = @"Hi";
NSString *longGreetingString =  @"Hi Again";
 /*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/

NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];

/*The mutableString, now uses an appendFormat to construct the string
     each %@ in the Parameters for the appendFormat is a place holder for values of NSStrings
     listed in the order you want after the comma. 
    Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:@"%@\n\n%@",shortGreetingString,longGreetingString];


NSLog (@"mutableString = %@" ,mutableString);

[pool drain];
莫言歌 2024-11-02 19:08:28

我认为这个 可能对你有帮助。你宁愿使用 '\r' 而不是 '\n'

我也遇到了类似的问题,发现 \n 在 LLDB 中有效,但在 GDB 中无效

I think this might help you. You'd rather to use '\r' instead of '\n'

I also had a similar problem and found \n works in LLDB but not in GDB

余生一个溪 2024-11-02 19:08:28

尝试使用 NSString。你可以使用:

NSString *a = [NSString stringWithFormat:@"%@\n\n%@", @"Hi", @"Hello again"]

Try using NSString. You could use:

NSString *a = [NSString stringWithFormat:@"%@\n\n%@", @"Hi", @"Hello again"]
煮酒 2024-11-02 19:08:28

如果您的字符串位于 UIView(例如 UILabel)中,您还需要将行数设置为 0

myView.numberOfLines=0;

If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0

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