How to break a string in Objective-C without breaking code

Xcode throws all kinds of errors when I insert a line break in a string. For example, this fails:

if (newMaximumNumberOfSides > 12) { NSLog(@"Invalid maximum number of sides: %i is greater than the maximum of 12 allowed.", newMaximumNumberOfSides); } 

But it works:

 if (newMaximumNumberOfSides > 12) { NSLog(@"Invalid maximum number of sides: %i is greater than the maximum of 12 allowed.", newMaximumNumberOfSides); } 

I would prefer the first one because it is cleaner to watch (short lines), but the code breaks. What is the best way to handle this? (Subquestion: does this refer to any of the syntax instructions? I searched all my books for "line breaks" with no effect.)

+4
source share
3 answers

All this should work:

 NSString *s = @"this" \ @" is a" \ @" very long" \ @" string!"; NSLog(s); NSString *s1 = @"this" @" is a" @" very long" @" string!"; NSLog(s1); NSString *s2 = @"this" " is a" " very long" " string!"; NSLog(s2); NSString *s3 = @"this\ is a\ very long\ string!"; NSLog(s3); 
+7
source
 if (newMaximumNumberOfSides > 12) { NSLog(@"Invalid maximum number of sides: %i is greater than " "the maximum of 12 allowed.", newMaximumNumberOfSides); } 
+8
source

String literals in C cannot contain newline characters. To quote http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html :

No string literal can end a string. Older versions of GCC adopted multiline string constants. Instead, you can use extended strings, or concatenate a string constant

Other answers provided give examples of both extended lines and string concatenation.

+2
source

Source: https://habr.com/ru/post/1341382/


All Articles