Swift Comments

Swift comments are notes written inside source code to explain intent, document APIs, organize files, or temporarily exclude code from compilation. The Swift compiler ignores ordinary comments, so they do not change the program’s output or execution.

Swift supports single-line comments, multiline comments, nested multiline comments, and documentation comments. This tutorial explains the syntax and shows when to use each form.

Swift Comment Syntax at a Glance

Comment typeSyntaxTypical use
Single-line comment// commentShort notes beside or above a statement
Multiline comment/* comment */Comments that span one or more lines
Documentation comment/// or /** ... */Documentation for types, properties, methods, and parameters

Swift Single-Line Comments with //

Start a single-line comment with two forward slashes, //. Everything after // on that line is treated as a comment.

</>
Copy
// This is a comment in Swift program

A single-line comment can appear on its own line or after a Swift statement.

</>
Copy
/* This is a comment in Swift program */

The second example uses multiline-comment delimiters on one line. It is valid Swift, but // is usually clearer for a short one-line note.

</>
Copy
let maximumAttempts = 3 // Number of allowed retry attempts
print(maximumAttempts)

Swift Multiline Comments with /* and */

Use /* to begin a multiline comment and */ to end it. The comment may span several lines, or it may be written on a single line.

</>
Copy
/* This is a line in the comment
 This is another comment line */

Multiline comments are useful when a note needs more context than a single line can provide.

</>
Copy
/*
 The total includes the item price
 and the applicable delivery charge.
 */
let total = itemPrice + deliveryCharge

Nested Multiline Comments in Swift

Swift allows one multiline comment inside another multiline comment. This is useful when you temporarily comment out a block of code that already contains /* ... */ comments.

</>
Copy
/*
 let subtotal = 500

 /* Apply the seasonal discount. */
 let discount = 50

 let finalAmount = subtotal - discount
 */

The outer comment does not end when the inner */ is reached. Swift tracks the nested comment and closes the outer comment only at the final delimiter.

Swift Documentation Comments with /// and /** */

Documentation comments describe declarations such as structures, classes, functions, methods, properties, and parameters. Use three forward slashes, ///, for line-based documentation or /** ... */ for block documentation. Development tools can display these comments as formatted API documentation and Quick Help.

</>
Copy
/// Returns the sum of two integer values.
///
/// - Parameters:
///   - first: The first integer.
///   - second: The second integer.
/// - Returns: The sum of `first` and `second`.
func add(_ first: Int, _ second: Int) -> Int {
    first + second
}

A documentation block can express the same information when a block style is easier to read.

</>
Copy
/**
 Creates a greeting for the supplied name.

 - Parameter name: The name to include in the greeting.
 - Returns: A greeting string.
 */
func greeting(for name: String) -> String {
    "Hello, \(name)!"
}

MARK, TODO, and FIXME Comments in Swift Files

Xcode recognizes several comment labels that help developers navigate and maintain Swift source files:

  • // MARK: creates a named section in the source file’s jump bar.
  • // TODO: records work that still needs to be completed.
  • // FIXME: identifies code that needs correction or review.
</>
Copy
// MARK: - User Validation

func isValid(username: String) -> Bool {
    // TODO: Add the complete validation rules.
    return !username.isEmpty
}

// FIXME: Handle network failures before release.

When Swift Comments Improve Code Quality

Comments are most useful when they explain information that the code cannot express clearly by itself. Good comments describe intent, constraints, assumptions, or non-obvious decisions.

  • Explain why a particular implementation was chosen.
  • Document public functions, parameters, return values, and thrown errors.
  • Record temporary limitations with a clear TODO or FIXME.
  • Use MARK comments to separate related declarations in a long file.
  • Update or remove comments when the related code changes.

Avoid comments that merely repeat an obvious statement. Clear naming is usually better than explaining unclear code with extra comments.

</>
Copy
// Less useful: repeats the code.
userCount += 1 // Add 1 to userCount

// More useful: explains the reason.
userCount += 1 // Include the administrator in the displayed total.

Common Swift Comment Mistakes

  • Missing the closing delimiter: Every /* comment must eventually be closed with */.
  • Using // for several long lines: A multiline comment may be easier to read when the note forms one continuous explanation.
  • Leaving outdated comments: A comment that contradicts the code is more harmful than no comment.
  • Commenting out code permanently: Remove obsolete code when version control already preserves its history.
  • Writing documentation without useful details: Describe behavior, parameters, return values, errors, or important constraints rather than repeating the declaration name.

Swift Comments Frequently Asked Questions

How do you comment a single line in Swift?

Place // before the text. Swift ignores everything after the two slashes until the end of that line.

How do you comment multiple lines in Swift?

Place the text between /* and */. The enclosed comment can span any number of lines.

Can Swift multiline comments be nested?

Yes. Swift supports nested multiline comments, so a /* ... */ block may contain another multiline comment.

What is the difference between // and /// in Swift?

// creates an ordinary source-code comment. /// creates a documentation comment associated with the declaration that follows it.

Do Swift comments affect program execution?

No. Ordinary and documentation comments are ignored during compilation and do not run as part of the program.

Swift Comments Editorial QA Checklist

  • Verify that every single-line example begins with //.
  • Verify that each multiline example has matching /* and */ delimiters.
  • Confirm that the nested-comment example closes both the inner and outer comments.
  • Confirm that documentation examples use /// or /** ... */ immediately before a declaration.
  • Check that MARK, TODO, and FIXME examples describe their Xcode-oriented purpose accurately.

Swift Comments Summary

Use // for single-line notes and /* ... */ for multiline comments. Swift also supports nested multiline comments. Use /// or /** ... */ when documenting declarations, and use MARK, TODO, and FIXME labels to organize maintenance work in Xcode.

In this Swift Tutorial, we have learned how to write single and multiple line comments in Swift programming.