通过多行分解长格式的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或任何代码编辑器中,是否可以避免有一个很长的string,必须通过在编辑器中滚动来读取?

有没有办法把它分解成多行? 我发现,如果我尝试这样做,代码将不会编译,因为编译器到达行的末尾,并没有看到该string的结尾引号( " )。

有没有人知道这个方法?

就在这里。 相邻的string将由编译器连接起来。

 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); 

这是比有用的答案更有趣的function,但…

  // 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) ,会导致: first linesecond linethird line...last line

现在考虑这个例子:

  // 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 first lineXsecond lineXthird lineX...Xlast line" X first lineXsecond lineXthird lineX...Xlast line" X first lineXsecond lineXthird lineX...Xlast line" ,那些讨厌的X将被4个空格replace(在这种情况下,tabulator有4个空格,我无法正确格式化,对不起)可以真正阻止你获得预期的结果。