In Swift, how do multiline string literals and raw strings with # delimiters change escaping, indentation and interpolation?
answer
- triple quotes
- the closing delimiter sets indentation
- number signs around the quotes
- escape needs matching number signs
basics
~20 sA triple-quoted multiline literal keeps line breaks and strips the indentation of its closing delimiter. Wrapping a literal in # signs makes backslashes and quotes literal; escapes and interpolation then need the same number of #, as in #(name).
solid answer
~40 sA **multiline string literal** starts and ends with `"""`, each on its own line. The line breaks next to the delimiters are not part of the string, the indentation before the closing `"""` is removed from every line, and a line indented less is a compile error. Quotes need no escaping inside, and a trailing backslash joins a line with the next. An **extended delimiter** wraps a literal in number signs, `#"…"#`: backslashes and quotes become plain text, so Windows paths and JSON fragments read as written. Escapes still work when they carry the same number of signs, so `\#(user)` interpolates and `\#n` is a newline, while a plain `\(user)` stays literal. The two combine as `#"""…"""#`.
code
swift · 15 lineslet user = "Zoë"
let path = #"C:\Users\\#(user)\chat-logs"#
print(path) // C:\Users\Zoë\chat-logs
let payload = #"{"name": "\#(user)", "template": "\(user)"}"#
print(payload) // {"name": "Zoë", "template": "\(user)"}
let welcome = """
Welcome, \(user)!
Indented two extra spaces.
No "escapes" needed for quotes. \
This continues the same line.
"""
print(welcome)go deeper
Recall the triple-quote syntax, that the closing delimiter's indentation is stripped, and that #"…"# makes backslashes literal while #( ) still interpolates.
Explain the indentation rule and its error, the trailing-backslash line join, and how the number of # signs must match in escapes.
Catch raw literals that silently fail to interpolate and multiline templates whose indentation leaks into output, and choose the form that keeps reviews readable.
Set a team convention for long literals, such as raw strings for paths and JSON fixtures and multiline literals for templates, so source matches output.
## Three forms of string literal Swift has one `String` type but several ways to write a literal for it: | Form | Delimiters | Typical use | |---|---|---| | Single-line | `"…"` | Short text, labels, keys | | Multiline | `"""` on its own line, at both ends | Message templates, SQL, help text | | Extended delimiter (raw) | `#"…"#`, also `##"…"##` and so on | Text full of backslashes or quotes | Extended delimiters combine with the multiline form, as `#"""` … `"""#`, and the rules below apply to both. ## Multiline literals: line breaks and indentation A multiline literal begins with `"""` followed by a line break and ends with a line break followed by `"""`. The Swift reference manual sets out how its content is read: - **The line break after the opening `"""` and the one before the closing `"""` are not part of the string.** To start or end with a newline, write a blank first or last line. - **The closing `"""` sets the indentation.** Every non-blank line must begin with exactly the indentation that appears before the closing delimiter, and that indentation is removed. Extra spaces beyond it stay in the string. - **Under-indenting is an error.** A line indented less than the closing delimiter fails with `insufficient indentation of line in multi-line string literal`, and there is no conversion between tabs and spaces. - **Line breaks are normalized** to line feeds, even if the source file uses carriage returns. - **A backslash at the end of a line** removes that line break, along with any whitespace between the backslash and the break, so you can hard-wrap a long line in source without changing the value. - **Quotes need no escaping** inside, though three in a row still end the literal unless you escape one or use an extended delimiter. Interpolation with `\(…)` and escapes such as `\n` work in multiline literals exactly as in single-line ones. ## Extended delimiters: raw strings Put one or more number signs before the opening quote and the same number after the closing quote, and the literal becomes **raw**: a backslash or a quote is just a character. That is what makes these readable: - a Windows path such as `#"C:\Users\chat-logs"#`; - a JSON fragment with embedded quotes, `#"{"name": "Zoë"}"#`; - a literal that must contain `"""`, written inside `#"""` … `"""#`. Escapes are not gone; they need the **same number of number signs** as the delimiter: 1. `\#(user)` interpolates inside `#"…"#`, while `\(user)` stays as the six characters `\(user)`. 2. `\#n` is a line feed inside `#"…"#`; inside `##"…"##` it would have to be `\##n`. 3. A sequence such as `\` followed by a quote is literal, so a raw string can end in a backslash before its closing `"#`. More signs exist for the rare text that itself contains `"#`; use the fewest that work. ## Choosing between the forms Use a plain literal until escaping makes it hard to read. Switch to multiline when the text has several lines a reader should see as lines, such as an email body or a long chat welcome message. Switch to an extended delimiter when the text is dense with backslashes or quotes, so that what you read in source is what ends up in the string. Combining both is normal for multiline templates that contain quotes and placeholders. A common review catch is a raw literal that tried to interpolate with `\(…)`: it compiles, and the output shows the placeholder text instead of the value. Another is a multiline literal whose content was pasted with less indentation than the closing delimiter; the compiler reports it, and the fix is to indent the content or move the delimiter. ## Errors you will see - `insufficient indentation of line in multi-line string literal`, for a content line indented less than the closing `"""`. - `invalid escape sequence in literal`, for an unknown escape such as `\q` in a non-raw literal; a raw literal accepts the same text without complaint. ## Summary Multiline literals control **line structure and indentation**; extended delimiters control **what a backslash or quote means**. Interpolation survives in both, spelled `\(…)` in normal literals and with matching number signs, such as `\#(…)`, in raw ones.
- In Swift, how do you include three double quotation marks inside a multiline string literal?Wrap the literal in an extended delimiter, `#"""` … `"""#`, so a run of three quotes inside it no longer ends the literal. Alternatively escape at least one of the quotes with a backslash in a normal multiline literal. The extended-delimiter form keeps the text readable when it is something like a code sample or documentation.
- Why might a Swift raw string print \(name) instead of the value of name?Inside `#"…"#` a backslash is literal unless it is followed by the same number of number signs as the delimiter. `\(name)` therefore stays as text; the interpolation must be written `\#(name)`. It compiles either way, so the bug shows only in output or in a test.
- A multiline literal keeps all leading spaces exactly as typed in the source.
- Interpolation is impossible inside a raw string with # delimiters.
- The line break right after the opening triple quotes is part of the string.
- Raw strings are a separate type from String in Swift.
- You still have to escape quotes inside a multiline string literal.