How to write comments in Swift language?

Comments explains about single or multiple lines of a code

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

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

  • Single line comments
  • Multi Line comments
  • Documentation commands

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

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

Single Line comments in Swift

These also called inline comments.

  • It always starts with double forward slashes (// ) symbol character and ends with line break.
  • It is an description or piece of text for single line of code.
  • These comments are not required at starts in begin of line, but also can write in middle or end of 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 a multi line comments

Multi line comments in Swift

These comments written in multiple line and also called block comments.

Here is a way we can write a multi line comments . Multi line Comments always starts with /* and followed by single or multiple line 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 a multiple lines of code and ignored by 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 */ )