Lesson 6Swift Basics
Comments
Comments in Swift
Comments are notes in your code that Swift ignores. They help explain what your code does to others (and your future self)!
Types of Comments
Single-line
// CommentMulti-line
/* ... */Documentation
/// DocWhen to Use Comments
- Explain WHY, not WHAT
- Document complex logic
- Leave TODO/FIXME notes
- Use MARK to organize sections
Special Comment Tags
// MARK: -Section divider// TODO:Something to do later// FIXME:Bug to fix
main.swift
// This is a single-line comment
print("Hello!") // Comment at end of line
/*
This is a multi-line comment.
It can span multiple lines.
Great for longer explanations!
*/
/// Documentation comment for functions
/// - Parameter name: The person's name
/// - Returns: A greeting string
func greet(name: String) -> String {
return "Hello, \(name)!"
}
// MARK: - Game Variables
var score = 0
var lives = 3
// TODO: Add more features here
// FIXME: This needs to be fixed later
// Good comment - explains WHY
// Using 60 fps for smooth animation
let framesPerSecond = 60
// Bad comment - just states the obvious
// let x = 5 // Set x to 5 (Don't do this!)
print(greet(name: "Swift"))Try It Yourself!
Add comments to explain a piece of code you wrote earlier. Use MARK to organize your code into sections!