in , ,

Swift Comments

Swift Comments
Swift Comments

In this tutorial, you will learn about Swift comments, why and how to use them.

In computer programming, comments are hints that we use to make our code more understandable.

Comments are totally disregarded by the compiler. They are meant for fellow programmers.

Documentation is a critical and significant important in coding. At the point when we write coding, in the event that we can add a note to reminder ourself, it will be extremely useful while examining the code at later time.

There are two different ways to add comments in Swift:

  • // – Single Line comments
  • // – Multiline Comments

Single Line Comment

In Swift, any line that begins with//is a single line comment. For instance,

// create a variable 
var name = "Sohail"

// print the value

Here, we have created two single-line comments:

  • // create a variable
  • // print the value

We can likewise use the single-line comment alongside the code.

var name = "swift" // name is a string

Multiline Comment

In Swift, any content between/* and */is a multiline comment. For instance,

/* create a variable
to store salary of employees

var salary = 10000

In the above example, we have used/*…*/to write the comment that extends out for numerous lines.

Use of Swift Comment

1. Make Code Easier to Understand

On the off chance that we write comments in our code, it will be simpler for future reference.

Likewise, it will be simpler for other developers to comprehend the code.

2. Using Comments for debugging

On the off chance that we get an error while running the program, we can comment the line of code that causes the error instead of eliminating it. For instance,


// print("Error Line )


Here, print(“Error Line) was causing an error so we have changed it as the comment. Presently, the program runs with no error.

This is the means by which comments can be an important debugging tool.

Note: Always use comments to clarify why we accomplished some different options from how we accomplished something. Comments shouldn’t be the substitute for the way to clarify poorly written code.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Colors

HTML Colors


HTML RGB and RGBA Colors