Swift Comments
Adding notes and documentation to your Swift code
💬 What are Swift Comments?
Comments in Swift are text notes that explain your code. They're ignored by the compiler but help you and other developers understand what your code does. Use them to document your logic and make code more readable.
// This is a single-line comment
print("Hello, World!") // Comment at end of line
Types of Swift Comments
Single-Line Comments
Use // for one-line notes
// This explains the next line
let name = "Swift"
Multi-Line Comments
Use /* */ for longer explanations
/*
This is a multi-line comment
that spans several lines
*/
Nested Comments
Swift allows comments inside comments
/*
Outer comment
/* Inner comment */
Still outer comment
*/
Documentation Comments
Use /// for code documentation
/// Calculates the area of a circle
func circleArea(radius: Double) -> Double {
return 3.14159 * radius * radius
}
🔹 Single-Line Comments
Use double slashes (//) to create single-line comments:
// This is a comment explaining the variable
let greeting = "Hello, Swift!"
let age = 25 // Age of the user
// You can use multiple single-line comments
// to create longer explanations
// like this example here
Output:
Hello, Swift!
🔹 Multi-Line Comments
Use /* */ to create comments that span multiple lines:
/*
This is a multi-line comment.
It can span multiple lines and is useful
for longer explanations or temporarily
disabling blocks of code.
*/
let message = "Comments make code readable"
/*
You can also use multi-line comments
to disable code temporarily:
let unusedVariable = "This won't run"
print(unusedVariable)
*/
Output:
Comments make code readable
🔹 Documentation Comments
Use triple slashes (///) for documentation that appears in Xcode's Quick Help:
/// Adds two numbers together
/// - Parameters:
/// - a: The first number
/// - b: The second number
/// - Returns: The sum of a and b
func addNumbers(a: Int, b: Int) -> Int {
return a + b
}
/// A simple greeting function
/// - Parameter name: The name to greet
func greet(name: String) {
print("Hello, \(name)!")
}
Output:
Hello, Swift Developer!
🔹 Best Practices for Comments
Follow these guidelines for effective commenting:
Good Comment Practices:
- Explain Why: Focus on the reason, not what the code does
- Keep Updated: Update comments when you change code
- Be Clear: Write comments that anyone can understand
- Document Functions: Use /// for public functions and classes
// Good: Explains the business logic
// Apply 10% discount for premium customers
let discountedPrice = originalPrice * 0.9
// Bad: Just repeats what the code does
// Multiply originalPrice by 0.9
let discountedPrice = originalPrice * 0.9
/// Validates user email format
/// - Parameter email: Email string to validate
/// - Returns: true if email format is valid
func isValidEmail(_ email: String) -> Bool {
// Use regex pattern for email validation
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: email)
}