Introduction¶
When it comes to placing large amounts of text into a string literal, it can be all too common to find your code becoming a little hard to read. Especially if there happen to be a lot of escape characters or string interpolated function calls within it.
Enter multiline String literals.
They can really clean things up, and although they're relatively simple to use, there are still times they can leave you scratching your head.
How to use a multiline string literal¶
To use multiline string literals, surround your text with three double quotation marks """
. The quotation marks need to be on their own line, and the string begins on the first line after the opening quotation marks, and ends on the line before the closing quotation marks.
The whitespace before the closing quotation marks """
indicates to Swift what amount of whitespace to ignore in all the other lines in the string. This means—as shown in a section further down—that strings can be indented to match the surrounding code:
let sandman = """
Exit Light,
Enter Night,
Take My Hand,
We're Off to Never Never Land
"""
print(sandman)
// Exit Light,
// Enter Night,
// Take My Hand,
//
// We're Off to Never Never Land
Line breaks at the start or end of multiline literals¶
The line breaks on the opening and closing quotation marks """
lines will not be included, so the following strings are the same:
let singleLineString = "Witty one-liner"
let multilineString = """
Witty one-liner
"""
If you do in fact want line breaks at the start or end of a string, just insert one as the first or last line:
let breaks = """
Break before this line.
Break after this line.
"""
print(breaks)
//
// Break before this line.
// Break after this line.
//
When you don't want line breaks as part of the string value¶
Sometimes you just want to use line breaks for better code readability but don't want them to appear in the strings value. This can be accomplished by appending a backslash \
to the end of any lines you don't want breaks:
let noBreaks = """
This line, \
this line, \
and this line all on the same line
"""
print(noBreaks)
// This line, this line, and this line all on the same line
Indenting multiline string literals¶
You can change the indentation level of multiline string literals to match surrounding code. The indentation level is determined by the white space before the closing quotation marks """
. The leading white space in the other lines in the string must, at the least match this. Any extra will be visible in the string value:
let multiLevelIndents = """
This
is
an
example
of
indenting
strings
"""
print(multiLevelIndents)
// This
// is
// an
// example
// of
// indenting
// strings
The following code will not compile due to the string value not being sufficiently indented:
let compileError = """
This line is not sufficiently indented.
This line is sufficiently indented.
"""
// Compile error
// Insufficient indentation of line in multi-line string literal
The ability to indent your multiline string literal to match the surrounding code is great, just remember to take care where you place the closing quotation marks """
.
let indented = """
This line will be indented
"""
let notIndented = """
This line will not be indented
"""
print(indented)
print(notIndented)
// This line will be indented
// This line will not be indented