How do I make a new line in swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, use the escape sequence \n inside a string to create a new line. This is the standard newline character used across all Apple platforms (iOS, macOS, watchOS, tvOS). Swift also supports multi-line string literals using triple quotes ("""), which preserve line breaks as written in source code. For building strings dynamically, \n can be inserted through string interpolation, concatenation, or appended to a mutable string.
The \n Escape Sequence
\n is the newline character (Unicode U+000A). When Swift encounters \n inside a string literal, it outputs a line break at that position.
Multi-Line String Literals
Swift's triple-quote syntax lets you write multi-line strings without \n:
The closing """ indentation determines how much leading whitespace is stripped. Content indented beyond the closing quotes keeps its extra indentation:
Continuation Lines
To break a long line in source code without inserting a newline in the output, end the line with a backslash:
String Interpolation with New Lines
Building Strings with New Lines
Concatenation
Appending to a Mutable String
Joining an Array
joined(separator:) inserts the separator between each element — useful for building multi-line output from collections.
Platform-Specific Line Endings
On Apple platforms, always use \n. The \r\n combination is only needed when generating text for Windows systems or certain network protocols (HTTP, SMTP, FTP).
New Lines in SwiftUI
In SwiftUI, Text views render \n as line breaks. For more control over spacing, use a VStack with separate Text views and .padding() or .spacing.
Special Characters Reference
All escape sequences use a backslash prefix. Inside multi-line string literals ("""), double quotes do not need escaping unless three appear consecutively.
Common Pitfalls
- Raw strings ignore escape sequences:
#"Hello\nWorld"#prints literallyHello\nWorld— the\nis not interpreted. To use escape sequences in a raw string, add matching pound signs:#"Hello\#nWorld"#. - Trailing newline in multi-line strings: The closing
"""on its own line adds a final newline. Place it on the same line as the last text to avoid it:"""last line""". - Windows line endings in files: If you read a file created on Windows, it may contain
\r\n. Usecomponents(separatedBy: .newlines)instead ofsplit(separator: "\n")to handle both\nand\r\ntransparently. - Whitespace in multi-line strings: The indentation of the closing
"""defines the baseline. Content must be indented at least as far as the closing quotes, or the compiler reports an error. - print() adds its own newline:
print("Hello")outputsHello\n— it appends a newline by default. To suppress it, useprint("Hello", terminator: "").
Summary
- Use
\nin string literals for newlines:"Line 1\nLine 2" - Use triple-quote
"""for multi-line strings that preserve source formatting - Use
joined(separator: "\n")to combine array elements with line breaks print()automatically appends a newline — useterminator: ""to suppress it- In SwiftUI,
Text("Line 1\nLine 2")renders line breaks as expected - On Apple platforms, always use
\n(LF) — use\r\nonly for Windows interop
Related reading
- How do I make an attributed string using Swift?
- How do I make an enum Decodable in Swift?
- How do I make UILabel display outlined text?
- How do I make WRAP_CONTENT work on a RecyclerView
- How do I modify the background color of a List in SwiftUI?
- How do I obtain crash-data from my Android application?
- How do I open developer tools on iOS Simulator?
- How do I open phone settings when a button is clicked?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.