将长格式的 NSString 分解为多行

发布于 2024-11-01 05:53:23 字数 512 浏览 3 评论 0原文

给出以下 Objective-C 代码行:

[NSString stringWithFormat:@"\n Elapsed Time  \n Battery Level:  \n Torque:  \n Energy Used  \n Energy Regenerated:\n Cadence: \n Battery Temp: \n Motor Temp: \n Incline: \n Speed MPH: \n Speed KPH:\n Avg Speed MPH: \n Avg Speed KPH:\n Distance Miles:\n Distance Km: \n Time Date Stamp:\n"];

在 Xcode 或任何代码编辑器中,是否可以避免出现必须通过在编辑器中滚动来读取的非常长的字符串?

有没有办法将其分成多行?我发现如果我尝试这样做,代码将无法编译,因为编译器到达行尾并且看不到字符串的右引号 (")。

有人知道吗?知道解决这个问题的方法吗?

Given the following line of Objective-C code:

[NSString stringWithFormat:@"\n Elapsed Time  \n Battery Level:  \n Torque:  \n Energy Used  \n Energy Regenerated:\n Cadence: \n Battery Temp: \n Motor Temp: \n Incline: \n Speed MPH: \n Speed KPH:\n Avg Speed MPH: \n Avg Speed KPH:\n Distance Miles:\n Distance Km: \n Time Date Stamp:\n"];

In Xcode or any code editor, is it possible to avoid having a very long string that must be read by scrolling across it in the editor?

Is there a way of breaking it up into multiple lines? I am finding that if I try to do this, the code will not compile, because the compiler reaches the end of a line and does not see the closing quotation mark (") for the string.

Does anyone know a way around this?

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

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

发布评论

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

评论(2

秋凉 2024-11-08 05:53:23

是的,有。编译器将连接相邻的字符串。

NSString *info = [NSString stringWithFormat:@"\n Elapsed Time  \n"
                      "Battery Level:  \n"
                      "Torque:  \n"
                      "Energy Used  \n"
                      "Energy Regenerated:\n Cadence: \n"
                      "Battery Temp: \n"
                      "Motor Temp: \n"
                      "Incline: \n Speed MPH: \n" 
                      "Speed KPH:\n"
                      "Avg Speed MPH: %f \n"
                      "Avg Speed KPH:\n"
                      "Distance Miles:\n"
                      "Distance Km: \n"
                      "Time Date Stamp:\n"];
NSLog(info);

Yes there is. Adjacent strings will be concatenated for you by the compiler.

NSString *info = [NSString stringWithFormat:@"\n Elapsed Time  \n"
                      "Battery Level:  \n"
                      "Torque:  \n"
                      "Energy Used  \n"
                      "Energy Regenerated:\n Cadence: \n"
                      "Battery Temp: \n"
                      "Motor Temp: \n"
                      "Incline: \n Speed MPH: \n" 
                      "Speed KPH:\n"
                      "Avg Speed MPH: %f \n"
                      "Avg Speed KPH:\n"
                      "Distance Miles:\n"
                      "Distance Km: \n"
                      "Time Date Stamp:\n"];
NSLog(info);
擦肩而过的背影 2024-11-08 05:53:23

这更像是一个有趣的功能,而不是一个有用的答案,但是......

    // your code goes with that indentation (1 tab = 4 spaces)
    NSString *myString = @"first line\
second line\
third line\
...\
last line";
    // next lines of codes

但是您确实必须注意缩进,在上面执行 NSLog(@"%@", myString) 会导致: 第一行第二行第三行...最后一行

现在考虑这个例子:

    // your code goes with that indentation (1 tab = 4 spaces)
    NSString *myString = @"first line\
    second line\
    third line\
    ...\
    last line";
    // next lines of codes

这将给出:first lineXsecond lineXthird lineX...Xlast line",其中那些讨厌的 X 将被 4 个空格替换(制表符在这种情况下有 4 个空格,我不能'没有得到正确的格式,抱歉)因此,额外的间距确实会阻止您获得预期的结果。

This is more of an interesting feature than an useful answer, but...

    // your code goes with that indentation (1 tab = 4 spaces)
    NSString *myString = @"first line\
second line\
third line\
...\
last line";
    // next lines of codes

But you really have to mind the indentation, doing NSLog(@"%@", myString) for above, would result in: first linesecond linethird line...last line.

Now consider this example:

    // your code goes with that indentation (1 tab = 4 spaces)
    NSString *myString = @"first line\
    second line\
    third line\
    ...\
    last line";
    // next lines of codes

this would give: first lineXsecond lineXthird lineX...Xlast line", where those nasty X's would be replaced by 4 spaces (tabulator had 4 spaces in this case, and I couldn't get right formatting, sorry). So, additional spacing can really stop you from getting expected results.

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