Swift programming for Beginners – Swift Comments

Hits: 17

(Swift for Beginners)

Swift Comments

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

A comment is a programmer-readable explanation or annotation in the source code of a program. It is ignored by the compiler when the code is compiled.

Comments are intended for person reading the code to better understand the intent and functionality of the program. It can be helpful while working on a team to clarify the purpose of the code for other team members or while working solely it can act as a reminder to own.

Swift Comment and its Types

In swift there are two types of comments:

1. Single Line Comment

In Swift, any line starting with two slashes // is a single line comment.Everything starting with two slashes // is ignored by the compiler.

You can find a simple example at the top of the playground:

//: Playground - noun: a place where people can play

It starts with two slashes // and provides meaningful explanation of the file as “: Playground – noun: a place where people can play”.

Example 1: Single Line Comment

//Sample program
//stores 3.14 in variable pi
let pi = 3.14159 

The above example contains two single line comment:

// Sample program
// stores 3.14 in variable pi

2. Multiline Comment

If your comment includes multiple line, you can enclose it inside /*...*/.

Multiline comments start with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/). Everything in between /* and */ is ignored by the swift compiler.

/* This is a multiline comment.
After you write multiline comment add * following by / to end it

Example 2: Multiline comment

/* Hardcoded pi value may be less accurate.
So you can calculate using in built data types for more accurate value
let pi = Double.pi

The above example contains a multiline comment.

/* Hardcoded pi value may be less accurate.
So you can calculate using in built data types for more accurate value

Things to remember

Although comments are meaningful to better understand the intent of the code written. Here are things you need to consider while writing it:

  1. Do not enclose comments with // on multiline although it is valid and compiler ignores those lines. Instead enclose it inside multiline comment /*...*/Example:
    //This is a comment.
    //Use it when necessary and precisely

    The above way to write comment is correct but not recommended because you need to write multiline comments if the comment is greater than one line. The better way to write is using multiline comment as:

    /* This is a comment.
    Use it when necessary and precisely
  2. Single line comment can be written in a separate line or along with code in the same line. However, it is recommended to use comments in a separate line.Example:
    let pi = 3.14159 //stores 3.14 in variable pi

    This way to write comments is valid. But it’s better to write comment in a separate line as:

    //stores 3.14 in variable pi
    let pi = 3.14159
  3. Even if you are a single developer in a team, and you are the only one writing the code, if it’s uncommented, you will have a hard time trying to figure its purpose in the program. So, use it precisely and provide a meaningful description.
  4. Make comment very simple and meaningful.
  5. Do not write unnecessary comments on your code.
  6. In most cases, use comments to explain ‘why’ rather than ‘how’.


Swift programming for Beginners – Swift Comments


Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!