Swift - Introduction Comments

Introduction

Comments in Swift are nonexecutable text.

You can use comments as notes, explanations, or reminders to someone who will be reading your code.

You can begin a single-line comment with two forward slashes //.

You can open a multi-line comment using a forward slash and an asterisk (/*) and close it using an asterisk followed by a forward slash (*/).

Multiline comments can be nested:

// this is a single-line comment 

/* this is a multiple- 
    line comment 
*/ 

/* this is a comment 

/* this is a comment inside another comment */ 

    still a comment! */ 
    

In Swift, as in most programming languages, you insert comments into your code using two forward slashes ( // ):

// this is a comment
// this is another comment

The // characters mark the line as a comment. The compiler ignores comments at compilation time.

If you have several lines of comments, it is better to use the /* and */ combination to denote a block of statements as comments. For example:

/*
this is a comment
this is another comment
*/

The two preceding lines are marked as a comment.

You can also nest comments, as shown in the following example:

Demo

// this is a comment

var myAge = 25//from w w  w .  j  a v  a 2 s  .  c  o m
var radius = 10.1
var circumference = 2 * 3.14 * radius
var strMyAge = "My age is " + String(myAge)

/*
this is a comment
this is another comment
*/

print(strMyAge)

Result

To comment the entire block of code, enclose everything within the /* and */ , as shown here:

Demo

/*

// this is a comment/*www .  ja  va 2 s. c  om*/
var radius = 10.1
var myAge = 25
var circumference = 2 * 3.14 * radius
var strMyAge = "My age is " + String(myAge)


/*
this is a comment
this is another comment
*/

print(strMyAge)

*/