Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break up long formatted NSString over multiple lines

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?

like image 543
Sabobin Avatar asked Apr 13 '11 15:04

Sabobin


People also ask

What is a multiline string Python?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string. Python's indentation rules for blocks do not apply to lines inside a multiline string.


1 Answers

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); 
like image 155
Joe Avatar answered Oct 06 '22 03:10

Joe