0

This seems to be a perfectly valid program:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSString* hey = @"hey"
    @"there";
    NSLog(@"%@",hey);

    [pool drain];
    return 0;
}

Compiling this in llvm gcc and apple llvm shows no warnings or errors. Why? It seems one should be warned about this, as I can only see this as chaos-inducing. Especially in a situation like this:

NSArray* a = [NSArray arrayWithObjects:@"one",
              @"two,
              @"three
              @"four",
              nil];

You'd expect four entries...but no! Tricky, huh? Is it because you may want to split your string definition across multiple lines?

schellsan
  • 2,164
  • 1
  • 22
  • 32

2 Answers2

2

It's a syntax for multi-line strings in Objective-C.

I cannot definitively answer why the language designer(s) designed it that way, but we can probably assume that they wanted the syntax for Objective-C strings to be analogous to the syntax for C strings.

That is, in C you can do the following for a multi-line string:

char *my_string = "Line 1 "
                  "Line 2";

And in Objective-C, you can do the following for a multi-line string:

NSString *my_string = @"Line1 "
                      @"Line2"; // The @ on this line is optional.

(Code snippets adapted from https://stackoverflow.com/a/797351/25295)

Community
  • 1
  • 1
Stephen Booher
  • 6,522
  • 4
  • 34
  • 50
1

Is it because you may want to split your string definition across multiple lines?

Yes. It is useful when you want to split the very long string for better code reading i.e. in NSLocalizedString key description.

Vadim
  • 9,383
  • 7
  • 36
  • 58