
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
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
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
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
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]
Benefits of Using Comments
Comments are not the mandatory part of the code but they should be placed within the code to explain complex logic, and make the code easier to understand and maintain for others. The following are the main benefits of using the comments:
- Make code easier to understand: Comments make the code easier to understand for future reference. It is also helpful to other developers to understand and maintain the code.
- For Debugging: Comments are very helpful to identify incomplete or buggy code when debugging. A single comment can be written if your code is incomplete or if you have a problem that needs to be addressed later.