Why this happens
Swift string literals that start with a single " must end on the same line. If you press Return inside "..." the compiler stops at the line break and reports unterminated string literal. This is intentional. Single-line literals keep escaping predictable and prevent accidental newlines in UI strings, JSON, or URLs.
That means this does not compile:
var text: String = "This is some text
over multiple lines"
Xcode shows unterminated string literal on the first line because the closing " was never found before the newline. Swift expects you to be explicit about line breaks with \n, or to use a literal type that is designed to span lines.
Since Swift 4 (SE-0168), Swift has a dedicated multiline literal using three double quotes """. It handles newlines, indentation stripping, and unescaped quotes for you. On older toolchains or when you build strings dynamically, you need to compose the value from several single-line literals.
How to fix it
1. Use triple-quoted multiline literals (Swift 4 and later)
This is the preferred fix for static text, SQL, JSON samples, or any block of text you write directly in source.
Wrap the content between """ delimiters. The opening and closing """ must be on their own lines. The newline right after the opening """ and the newline right before the closing """ are not included in the value.
var text = """
This is some text
over multiple lines
"""
print(text)
// This is some text
// over multiple lines
How indentation works:
- Put the closing
"""on its own line. Its indentation sets the baseline. - Swift strips exactly that amount of leading whitespace from every line in the block.
- Content indented more than the baseline keeps the extra spaces. Content indented less is an error.
let message = """
Hello,
indented line keeps 4 extra spaces
Back to baseline
"""
Result:
Hello,
indented line keeps 4 extra spaces
Back to baseline
Other rules you will use often:
- Newlines are preserved. No need for
\nunless you want an explicit blank line at the end. - You can write
"without escaping. Write"""inside the block as\"""or break the literal. - Backslashes still escape. Use
\n,\t, or\(interpolation)as normal. To show a literal backslash, write\\. - To suppress the newline at the end of a line, end the line with
\:
let folded = """
This line ends with a backslash \
and continues without a newline.
"""
// "This line ends with a backslash and continues without a newline."
For strings that contain many backslashes or quotes, combine multiline with raw strings using #:
let regex = #"""
Pattern: "\d+" matches digits
Path: C:\Users\Example
"""#
With #""" you do not need to double-escape backslashes, and interpolation becomes \#(value).
2. Concatenate single-line literals with + and \n
Use this when you must support Swift 3 or earlier, or when you want full control over where newlines appear. Each piece is a normal "..." literal, so you add \n explicitly.
var text = "This is some text\n"
+ "over multiple lines\n"
The + operator is evaluated at compile time for literal strings, so there is no runtime cost. You can also break a long literal without adding a newline by omitting \n on intermediate lines:
let long = "This is a very long string that "
+ "we split for readability "
+ "but keep on one line."
// "This is a very long string that we split for readability but keep on one line."
If you need interpolation, it works per segment:
let name = "Ada"
let greeting = "Hello, \(name)\n"
+ "Welcome back.\n"
This approach is verbose for large blocks, but it is explicit and works in every Swift version.
3. Join an array of lines or use a small helper
When text is assembled from parts or you want to avoid + chains, build an array and join it. This is clearer than manual + and avoids trailing \n mistakes.
let lines = [
"This is some text",
"over multiple lines"
]
let text = lines.joined(separator: "\n")
You can change the separator without editing every line:
let csv = ["a", "b", "c"].joined(separator: ", ")
// "a, b, c"
The Stack Overflow extension in the source material does the same thing with variadic initializers. A simpler, standard-library version is enough in most projects:
extension String {
init(lines: String..., separator: String = "\n") {
self = lines.joined(separator: separator)
}
}
let hello = String(lines: "Hello", "World!")
// "Hello\nWorld!"
let list = String(lines: "Hello", "World!", separator: ", ")
// "Hello, World!"
Prefer joined(separator:) over a custom loop that appends with += in a loop. joined is optimized and avoids repeated allocations. Use the extension only if you want the String(lines:) call-site syntax across your codebase.
Choose which fix to use:
- Static block of text in source ->
"""multiline literal. - Need to support very old Swift or generate separators dynamically ->
joined(separator:)or+concatenation. - Text with many backslashes, regex, or JSON ->
#"""raw multiline literal.
How to prevent it
- Always use
"""for blocks longer than one line. Do not press Return inside"...". - Keep the closing
"""aligned with the opening code indentation so stripping is predictable. Let Xcode re-indent the block after you type the closing delimiter. - If you need an exact trailing newline, add a blank line before the closing
""". Otherwise Swift trims the final newline. - For localized strings, put multiline content in
Localizable.stringsorLocalizable.xcstringsinstead of hard-coding large literals. UseString(localized:)to load them. - Enable the SwiftLint rule
multiline_literal_bracketsor similar formatting checks to catch misaligned"""in code review.
FAQ
Why does my multiline string have extra leading spaces?
Swift strips only the indentation of the closing """. If you indent the content more than the closing delimiter, those extra spaces are kept. Align the closing """ with the least-indented content line, or dedent the content to match.
Do I need \n inside """?
No. Every line break you type inside """ becomes \n in the value. Use \n only if you want an additional blank line or you are concatenating single-line "..." literals with +.
Can I interpolate values inside a multiline literal?
Yes. Use \(value) inside """ and \#(value) inside #""" raw literals. The interpolation can span expressions and even contain newlines:
let count = 3
let summary = """
You have \(count) items:
\(lines.joined(separator: "\n"))
"""
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.