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 iOS platform, then you have to import UIKit and the program structure will have sligh changes in it and will look like this:

import UIKit

var str = "Hello W3schools"

println(str)

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 uses semi colon 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 compile 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 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

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.