How to write comments in Swift language?

Comments explain single or multiple lines of a code

Comments are written for a developer to understand the code better. These are ignored at runtime.

In Swift, You can write a comment in different following ways.

  • Single-line comments
  • Multi-Line comments
  • Documentation commands

Swift comments are similar to C Comments. It is always good practice to add Comments to the below blocks or lines of code.

  • Classes description
  • before the method declaration
  • Any complex logic code
  • documentation comments

Single Line comments in Swift

These are also called inline comments.

  • It always starts with double forward slashes (// ) symbol characters and ends with a line break.
  • It is a description or piece of text for a single line of code.
  • These comments are not required at the start in beginning of the line, but also can write in the middle or end of the line Syntax:
// This is single line comments in Swift Code

Example

hello.swift

// Hello World application
print("Hello World application") 

Learned single-line comments, How do you write multi-line comments

Multi-line comments in Swift

These comments are written in multiple lines and are also called block comments.

Here is a way we can write a multi-line comment. Multi-line Comments always start with /* and followed by single or multiple lines and end with */

/* multi line comments 1
 multi line comments 2
 multi line comments 3 */

You can also write multi line comments with single-line comment syntax as given below.

// multi line comments 1
// multi line comments 2
// multi line comments 3

This allows you to write multiple lines of code and be ignored by the compiler.

Block level comments in Swift Example:

// Hello world sample program
// Basic program to print hello world to console
/* First code to write in Swift programming language */

    print("Hello World app");

Conclusion

You learned different types of comments in Swift programming language.

  • single line comments (//)
  • Multiple line comments (Starting line begins with /* and ending line ends with */ )