Skip to main content

Swift Comments

Comment is not executable documented text. Which are used to describe logical thinking and code explanation by programmer. This text is ignored by compiler when compile the source code. This is helpful for programmer those are read our code and try to understanding implementation logic.

Type of Swift Comments

swift comment are similar to C, C++, Java and C# program. There is divided into two types. Single line comment and multi line comment.

Single line comment are start with double forward-slashes //. This can be used to short description of code.

//This is an single line comment text

That can be used in following way.

//Define constant variables
let pi=3.141592 , days=365

//display number of days
print("\(days)") //print 365

Multiline comment are start of forward-slashes and an asterisk /* . And end by asterisk and forward-slashes */. and inside of it mentioning and include comments text.

/*
  Function  : sum
  Parameters: Accepted two integer value
  return    : Result of two integers
*/
func sum(num1:Int,num2:Int)-> Int{
  return num1+num2
}

Swift compiler is strongly check multiline text opening and closing syntax. When we are defining of nested multiline comment are of their tag should be properly opening and closing.

/*
    Outer multiline text
  /*
  inner multiline text 
  */
*/

In case when missing of nested closing tag multiline the compiler are produced an error. See this example.

/*
    Outer multiline text
  /*
  inner multiline text 
  
*/

Error

test.swift:6:3: error: unterminated '/*' comment
*/
  ^
  */
test.swift:1:1: note: comment started here
/*
^




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment