Basic Syntax of Swift


In this Swift tutorial, we are going to discuss the basic syntax of Swift programming language.

Let’s start with the popular Hello, World! program:

/* Define the variable and print it */
var myStr = "Hello, World!"

print(myStr)

Run this code online

And the output will be:

Hello, World!

Using of Semicolon

In Swift, it is not necessary to use a semicolon (;) after each statement in the code. But it is optional. That means, if you use a semicolon after a statement, the compiler will not show any kind of error.

But in case you want to use multiple statements in the same line, you must have to use semicolons. Below is the example of this situation where two statements are in the same line:

var myStr = "Hello, World!"; print(myStr)

In the above piece of code, you can see we have separated two statements using a semicolon.

Identifiers

Identifiers refer to the name to recognize any kind of user-defined things, for example, it can be a variable, function, work, etc.

As Swift is a case-sensitive language, the name of an identifier is also case-sensitive. For example, if you define two variables with the name “myStr” and “MyStr” accordingly, then they are two different variables. “M” and “m” are two different things. See the example below:

var myStr = "Hello, World!"
var MyStr = "My String"
print(myStr)
print(MyStr)

Run this code online

And the output will be

Hello, World!
My String

An identifier can be started with an alphabet a-z or A-Z, or an underscore with letters, underscores, and digits. But using special characters such as %, @, # are not allowed within an identifier.

Below are some of the examples that can be used as identifiers:

firstname, cityName, _color, user_id, name43, a

Swift Keywords

Keywords are reserved words in a programming language. These are pre-defined with pre-defined meanings for specific operations.

In Swift also, there are keywords available. Each keyword has different types of functionality. These keywords can not be used as the name of any identifier. That means you can not use a keyword to give the name of a variable or function or any type of user-defined thing.

Below is given a list of some of the keywords available in Swift:

ifforClass
FuncLetprivate

Comments in Swift

Comments in the Swift program are those lines that will be completely ignored by the compiler and will not compile.

Now you may think about what is the usage of comments then.

Well, these comments are used as helping text that helps programmers to understand how the program works. A single-line comment can be started with // . In the case of multiline comments, it starts with /* and ends with */.

Below is the example of a single-line comment

// This line is comment

Below is multi-line comments

/* This is the comment
This is the comment part of next line */

Even single line comments also start with /*

/* This line is comment */

Below is how comments used in the program:

// This line is comment

var myStr = "Hello World"

/* This is multi line comments
 This is the next line */

print(myStr)

Output:

Hello World

Run this code online

Tokens in Swift

Tokens in Swift are the lowest level building blocks. Actually, a Swift program is the collection of tokens that are smallest building blocks.

A token consists of an identifier, keyword, punctuation, literal, or operator. The code snippet given below consists of 3 tokens:

print("Hello!")

print("World!")

var myStr = "SwiftSpeedy"

Whitespaces

In Swift, there are mainly two uses in Whitespaces:
 to separate the tokens in the source file and to distinguish between prefix, postfix, and infix operators, but is otherwise ignored.

Whitespace help to separate one statement from another and it enables the compiler to identify where one element in a statement ends and the next element starts.

Now look at the following statement:

var firstName

In this statement, there must be used one whitespace between “var” and “firstName”. Without the whitespace, we can’t declare the variable.

Printing in Swift

Using the print() function, we can show data directly in the console. At the very beginning of this tutorial, we have already used the print() function.

Example:

print("Hellow World")

Output:

Hellow World

Literals

A Literal in Swift is the representation of the source code of the value of a particular data type such as integer, string, floating-pont. Below are some of the examples of literals:

58               // Integer literal
9.71485          // Floating-point literal
"Hello World"    // String literal
TRUE             // Boolean literal

Import

The import keyword is used to import any module or framework in the Swift program file.

import UIKit

Function

A function in a program consists of one or multiple statements to perform a specific task. Below is the syntax of a function in Swift:

func funcname(Parameters) -> returntype {
   Statement1
   Statement2
   
   return variable
}

Below we have defined a function and using it:

func firstName(name: String) -> String {
   return name
}
print(firstName(name: "Taylor Swift"))