将长格式的 NSString 分解为多行
给出以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,有。编译器将连接相邻的字符串。
Yes there is. Adjacent strings will be concatenated for you by the compiler.
这更像是一个有趣的功能,而不是一个有用的答案,但是......
但是您确实必须注意缩进,在上面执行
NSLog(@"%@", myString)
会导致:第一行第二行第三行...最后一行
。现在考虑这个例子:
这将给出:
first lineXsecond lineXthird lineX...Xlast line"
,其中那些讨厌的 X 将被 4 个空格替换(制表符在这种情况下有 4 个空格,我不能'没有得到正确的格式,抱歉)因此,额外的间距确实会阻止您获得预期的结果。This is more of an interesting feature than an useful answer, but...
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:
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.