gpt4 book ai didi

iphone - 如何在不破坏代码的情况下在 Objective-C 中拆分字符串

转载 作者:可可西里 更新时间:2023-11-01 05:01:36 25 4
gpt4 key购买 nike

当我在字符串中插入换行符时,Xcode 会抛出各种错误。例如,这失败了:

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);

关于iphone - 如何在不破坏代码的情况下在 Objective-C 中拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5123911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com