Swift - Comments



Comments are the special text in the programs that are not compiled by the compiler. The main agenda of the comments is to explain to us what is happening in the specific line of code or in the whole program. Programmers generally add comments to explain the line of codes in the program.

Or we can say that comments are non-executable text and they are like a note or reminder to the user or programmer. In Swift, we can define comments in three different ways −

  • Single line comments

  • Multi-line comments

  • Nested Multi-line comments

Single Line Comment in Swift

A single-line comment is used to add only one-liner text in the code. A single-line comment begins with double forward slashes (//). The compiler or interpreter always ignores them and does not affect the execution of the program.

Syntax

Following is the syntax of single line comment −

// Add your comment here

Example

Swift program to add a single-line comment. Here we add a single-line comment in the program to explain the working of the for-in loop.

import Foundation
let num = 7
let endNum = 10

// For loop to display a sequence of numbers
for x in num...endNum{
   print(x)
}

Output

7
8
9
10

Multi-Line Comment in Swift

Multi-line comments are used to display multiple lines of non-executable text in the program to explain the working to the specific line of code or to add warnings, notes, etc. by the developers. Like other programming languages, in Swift, the multi-line comments begin with a forward slash followed by an asterisk(/*) and end with an asterisk followed by the forward-slash (*/).

Syntax

Following is the syntax of multi-line comment −

/* Add your 
Mult-line comment here */

Example

Swift program to add multi-line comments. Here we add multiple lines of comment in the program to explain how to add two arrays of the same length.

import Foundation

let arr1 = [1, 4, 6, 2]
let arr2 = [3, 5, 2, 4]

var result = [Int]()

/* Check the length of the array.
If they are equal then we add them using the + operator 
and store the sum in the result array */
if arr1.count == arr2.count {
   for i in 0..<arr1.count {
      let sum = arr1[i] + arr2[i]
      result.append(sum)
   }
   print(result)
} else {
   print("Arrays must of same length.")
}

Output

[4, 9, 8, 6]

Nested Multi-Line Comment in Swift

Starting from Swift 4 a new feature is also included in the multi-line comment that is nested multi-line comment. Now you are allowed to nest or add a multi-line comment inside another multi-line comment. It can easily comment out many blocks, even if the block contains multi-line comments.

Syntax

Following is the syntax of nested multi-line comments −

/* Add your multi-line comment.
/* Add your nested multi-line comment. */
End multi-line comment */

Example

Swift program to add nested multi-line comments. Here we add nested multiple lines of comment in the program to add an alternative code of adding two arrays.

import Foundation
let arr1 = [1, 4, 6, 2]
let arr2 = [3, 5, 2, 4]

var result = [Int]()
/* Checks the length of the array.
If they are equal then we add them using the + operator 
and store the sum in the result array 
/*You can also use the following code to add two arrays:
if arr1.count == arr2.count {
   let result = zip(arr1, arr2).map(+)
   print(result)
*/
*/
if arr1.count == arr2.count {
   for i in 0..<arr1.count {
      let sum = arr1[i] + arr2[i]
      result.append(sum)
   }
   print(result)
} else {
   print("Arrays must of same length.")
}

Output

[4, 9, 8, 6]
Advertisements