Swift Basic Syntax

Till now we have seen how to set up the environment to write, compile and execute code along with a simple Hello program.

If you want to write the above code snippet for the iOS platform, then you have to import UIKit and the program structure will have slight changes in it and will look like this:

import UIKit

var str = "Hello W3schools"

println(str)

An import statement in Swift is used to import any Objective-C framework or library directly within your Swift program. Swift programmers can also add C and C++ into Swift applications.

NOTE: Like other programming languages like C, Java, etc., Swift does not require any semicolon after each statement. But it is optional, and if programmers use a semicolon after every statement, the compiler does not show any error message.

Comments in Swift

Comments are those lines in Swift program which do not get compiled by the Swift compiler at the time of compilation. They act as helping texts for programmers to understand the working of the code snippet and are ignored by the compiler. Multiline comments start with /* and terminate with */ and in between the strings that will act as a comment.

Example:
import UIKit

/* This is the comment section */
var str = "Hello W3schools"

println(str)

/* This is another comment :: end of code */

Multiline comments can be nested like this:

/* My first program in Swift!

/* Hello, W3 */

Single line comments can be written using double slash.

Example:
// Hello W3schools