44

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?

Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • Hey you missed to pass %f float value @ end. Avg Speed MPH: %f \n Avg Speed KPH:\n................ Even this way worked for me – Ajay Sharma Apr 13 '11 at 17:21

2 Answers2

97

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);
Joe
  • 56,979
  • 9
  • 128
  • 135
  • 1
    Make sure you add `@` before all quoted strings – XJones Apr 13 '11 at 15:51
  • 2
    @XJones It compiled and printed with only 1 @ at the begning of the string as per @Joe answere. – Sabobin Apr 13 '11 at 17:01
  • 3
    @Seamus the code was tested and worked as posted and did not need to be edited. It works with or with out the @ on every line as long as the @ is on the first line. See the example here http://www.cocoadev.com/index.pl?NSString – Joe Apr 13 '11 at 17:06
  • 1
    I'm just used to adding the `@` on all lines, but yes, I see that it works without it. +1 for @Joe. – XJones Apr 13 '11 at 17:14
  • This works in console but not in sectionIndexTitle for tableview – Prince Kumar Sharma May 10 '12 at 13:40
  • @Princekumar ? This works regardless, the compiler concatenates the string. The result will be an `NSString` and `sectionIndexTitle` will in no way be affected by the concatenation. – Joe May 10 '12 at 13:55
  • ok, but I want the long words display as multiple line in sectionIndexview would u please help me regarding this. link is http://stackoverflow.com/questions/10328561/is-it-possible-to-wrap-the-word-in-uitableviewsectionindex/10518995#10518995 – Prince Kumar Sharma May 10 '12 at 14:02
18

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.

matm
  • 7,059
  • 5
  • 36
  • 50