Swift Comments

Swift Introduction

💬 Swift Comments (Complete & Easy Guide)

In Swift Comments are used to explain code, make notes, or temporarily disable code.

They are ignored by the Swift compiler and do not affect program output.


 1. Single-Line Comment

Used for short explanations.

// This is a single-line comment
print("Hello, Swift!") // This prints text

✔ Starts with //


 2. Multi-Line Comment

Used for long descriptions.

/*
This is a
multi-line comment
in Swift
*/

✔ Starts with /*
✔ Ends with */


 3. Nested Comments (Special Feature 🔥)

Swift allows comments inside comments (many languages don’t!).

/*
This is an outer comment
/*
This is a nested comment
*/
*/

✔ Very useful for debugging large blocks


 4. Documentation Comments (Doc Comments)

Used to generate official documentation.

Single-line Doc Comment

/// Calculates square of a number
func square(_ num: Int) -> Int {
return num * num
}

Multi-line Doc Comment

/**
This function adds two numbers.
- Parameters:
- a: First number
- b: Second number
- Returns: Sum of two numbers
*/

func add(a: Int, b: Int) -> Int {
return a + b
}

✔ Appears in Xcode Quick Help (⌥ + Click)


 5. Commenting Out Code (Debugging)

Temporarily disable code without deleting it.

// print("This line will not run")

/*
print(“Line 1”)
print(“Line 2”)
*/


 6. Best Practices for Comments

✅ Explain why, not obvious what
✅ Keep comments short & clear
✅ Use doc comments for functions
❌ Don’t over-comment simple code


🧠 Summary Table

Comment TypeSyntaxUse
Single-line//Short notes
Multi-line/* */Long notes
Nested/* /* */ */Advanced usage
Doc comment/// or /** */Documentation

You may also like...