[NSString stringWithFormat:@"%@ \n%@ \n%@",
self.message1,self.message2,self.message3];
有一种方法只传递 message1 ..2 ..3 如果不是 == "" ?
[NSString stringWithFormat:@"%@ \n%@ \n%@",
self.message1,self.message2,self.message3];
有一种方法只传递 message1 ..2 ..3 如果不是 == "" ?
只是建议一个替代和更紧凑的解决方案:
[[@[self.message1, self.message2, self.message3]
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]
componentsJoinedByString:@" \n"];
因此,与 Mundi 的解决方案不同的是:
There are obviously more steps in there so it'll cost a bit more but for most purposes you really don't need to care about that nickel and dime stuff.
如果您通过@""
,则无论如何都不会显示任何内容,因此这不是问题。所以我假设如果字符串为空,您也不希望换行符。
NSMutableString *s = [NSMutableString string];
if (self.message1 && self.message1.length) {
[s appendFormat:@"%@ \n", self.message1];
}
if (self.message2 && self.message2.length) {
[s appendFormat:@"%@ \n", self.message2];
}
if (self.message3 && self.message3.length) {
[s appendFormat:@"%@ \n", self.message3];
}