Iphone 如何在不破坏代码的情况下拆分Objective-C中的字符串
当我在字符串中插入换行符时,Xcode会抛出各种错误。例如,这失败了:Iphone 如何在不破坏代码的情况下拆分Objective-C中的字符串,iphone,objective-c,xcode,ios,Iphone,Objective C,Xcode,Ios,当我在字符串中插入换行符时,Xcode会抛出各种错误。例如,这失败了: if (newMaximumNumberOfSides > 12) { NSLog(@"Invalid maximum number of sides: %i is greater than the maximum of 12 allowed.", newMaximumNumberOfSides); } 但这是可行的: if (newMaximumNumberOfSides >
if (newMaximumNumberOfSides > 12) {
NSLog(@"Invalid maximum number of sides: %i is greater than
the maximum of 12 allowed.", newMaximumNumberOfSides);
}
但这是可行的:
if (newMaximumNumberOfSides > 12) {
NSLog(@"Invalid maximum number of sides: %i is greater than the maximum of 12 allowed.",
newMaximumNumberOfSides);
}
我更喜欢前者,因为它看起来更干净(行更短),但代码会中断。处理这个问题的最好方法是什么?(子问题:这在任何语法指南中都有引用吗?我在我所有的书中搜索了“换行符”都没有效果。)所有这些都应该有效:
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);
C中的字符串文字不能包含换行符。引述: 任何字符串文字都不能超过 行尾。GCC的旧版本 接受的多行字符串常量。 您可以使用连续的行, 或字符串常量串联 已经给出的其他答案给出了连续行和字符串连接的示例
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);