Comments in represent text that is useful to the programmer or developement team. Think of them as notes or reminders to yourself of important concepts or ideas that need a little explaining above the syntax of the raw code.

In Swift there are two kinds of comments: single-line comments and multi-line comments. These comments will be completely ignored by the compiler at compile-time.

Famous software engineer Martin Fowler once wrote,

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

Single-Line Comments

In Swift, as in many other languages, single line comments are prefaced with two consecutive forward slashes (//). They don’t have to be their own line, single line comments can be appended at the end of a line to give more context.

// I am a single-line comment!
print("Hello World")

A single-line comment can also be used to comment after a line of code:

print("Hello World") // Me, too!

Multi-Line Comments

Multi-line comments allow for greater context and readability of a concept or idea that your program is doing. It is also commonly used for metadata information like authorship and copyright information. In Swift, multi-line comments are made opened with /* and closed with */.

This space can be used to write notes to our future selves about ideas that we want
to implement and just haven't or if our algorithm isn't easily legible through
the code, we can explain it here. This is especially important if we are part
of a team separated by time and space and communication occurs here instead of an office.
Edit this page on GitHub


Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Swift on Codecademy

Edit this page on GitHub