Swift Comments
In this article, we'll look at how to write comments in Swift.
A program comment is like a note you can write inside your code. It is ignored when the program runs, so it won't affect execution.
Adding appropriate explanations or reminders as comments makes your code easier to understand, both for other people who read it and for yourself when you come back to it later.
You can also use comments to temporarily disable (comment out) parts of your code when you don't want them to run.
Single-Line Comments in Swift
In Swift, you can write a single-line comment by starting the line with //. Everything after // will be treated as a comment.
// This is a comment.
print("Hello Swift!")
In the example below, only print("Hello Swift!") is executed, because everything after // is ignored.
print("Hello Swift!") // This is a comment.
Multi-Line Block Comments in Swift
To write a multi-line comment in Swift, wrap the text you want to comment out between /* and */.
/* This is an example of a multi-line comment.
Line 2 of the comment
Line 3 of the comment */
print("Hello Swift!")
Multi-line comments can also be nested. This is useful when you want to comment out a larger block of code that already contains comments.
/* This is an example of a multi-line comment.
Line 2 of the comment
/* This is a nested comment */
Line 3 of the comment */
print("Hello Swift!")