Swift Tutorial
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Swift - Comments



The Comments are added in programming code with the purpose of making the code easier to understand. It makes the code more readable and hence easier to update the code later. Comments are ignored by the compiler while running the code. In Swift, there are two ways of putting a comment.

  • Single line comment
  • Multi-line comment

Single line Comment

It starts with # and ends with the end of that line. Anything after # to the end of the line is a single line comment and will be ignored by the compiler.

Example:

The example below illustrates how to use single line comments in a Swift code. The compiler ignores the comments while compiling the code.

# first line comment
puts("Hello World!.") # second line comment

The output of the above code will be:

Hello World!.

Multi-line Comment

It starts with =begin and ends with =end. Anything between =begin and =end is a multi-line comment and will be ignored by compiler.

Syntax

=begin
comment lines
=end

Example:

The example below describes how to use multiple line comments in a Swift code which is ignored by the compiler while compiling the code.

=begin
comment line 1
comment line 2
=end

puts("Hello World!.") 

=begin
comment line 3
comment line 4
=end 

puts("Programming is fun.") 

The output of the above code will be:

Hello World!.
Programming is fun.

5