Swift Comments
Swift Comments
Use comments to explain code.
Swift supports single-line, multi-line (nestable), and documentation comments.
Single-line and Multi-line
Use // for single-line comments and /* ... */ for multi-line comments.
Example
// This is a single-line comment
print("Hello")
/* This is a
multi-line comment */
print("Swift")
/* Nested comments are allowed:
/* inner */
*/
This example shows single- and multi-line comments.
Documentation Comments
Use /// before declarations to generate documentation, e.g. for functions, types, and properties.
Documentation comments can include parameters, return values, and more.
They differ from regular comments in that they are processed by the compiler and can be used to generate documentation.
Example
/// Returns the sum of two integers.
/// - Parameters: a, b
func add(_ a: Int, _ b: Int) -> Int { a + b }
print(add(2, 3))
This example uses triple-slash comments to document a function.
Note: Multi-line documentation comments are started with /** and ended with */.