我可以在字符串插值表达式中写文字吗?怎么办?
“项目数:\(items > 0 ? items :
"zero"
)"
最佳答案
documentation似乎暗示转义内插字符串中的文字在某种程度上是可能的
The expressions you write inside parentheses within an interpolated string cannot contain an unescaped double quote (") or backslash (), and cannot contain a carriage return or line feed.
但是,据我所知,目前没有办法。
可能的解决方法是字符串连接:
"Number of items: " + (items > 0 ? "\(items)" : "zero")
或者简单地使用一个变量
let nOfItems = items > 0 ? "\(items)" : "zero"
"Number of items: \(nOfItems)"
关于swift - 字符串插值表达式中的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997790/