Swift 的
String类型与 Foundation 的NSString类桥接。 Foundation 还扩展了String以公开NSString定义的方法。这意味着,如果您导入 Foundation,您可以访问String上的那些NSString方法,而无需进行强制转换。
-
当源代码在多行字符串文字中包含换行符时,该换行符也会出现在字符串的值中。如果您想使用换行符使源代码更易于阅读,但又不希望换行符成为字符串值的一部分,请在这些换行符的末尾写入反斜杠 (
\)线路let softWrappedQuotation = """ The White Rabbit put on his spectacles. "Where shall I begin, \ please your Majesty?" he asked. "Begin at the beginning," the King said gravely, "and go on \ till you come to the end; then stop." """ -
要制作以换行符开头或结尾的多行字符串文字,请编写一个空行作为第一行或最后一行。例如:
let lineBreaks = """ This string starts with a line break. It also ends with a line break. """ print(lineBreaks) -
由于多行字符串文字使用三个双引号而不是一个,因此您可以在多行字符串文字内包含双引号 (
"),而无需转义它。要在多行字符串中包含文本""",请转义至少一个引号。例如:let threeDoubleQuotationMarks = """ Escaping the first quotation mark \""" Escaping all three quotation marks \"\"\" """ output Escaping the first quotation mark """ Escaping all three quotation marks """ -
换行
let line = #"Line 1\#nLine 2"# print(line) output Line 1 Line 2