Comments
Comments in Swift represent text that is ignored by the compiler at-compile time, but is useful to the programmer or development team. They are ideal for documenting important concepts or ideas that require explanation beyond the syntax of the raw code. Both single-line and multi-line comments are supported in Swift.
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, single-line comments are prefaced with two consecutive forward slashes (//
). They don’t have to be their own line and 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 the 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 wantto implement and just haven't or if our algorithm isn't easily legible throughthe code, we can explain it here. This is especially important if we are partof a team separated by time and space and communication occurs here instead of an office.*/
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours