Swift Basic Syntax (Comments, Keywords, Semicolons)

Generally, the swift programming playground editor will contain two portions like as shown below. The left portion of the editor is to write the code and the right side portion is used to see the result of coding.

 

Swift Playground Interface to Create Applications

 

If you observe above playground editor by default it contains small pieces of code like as shown below.

 

//: playground - noun: a place where people can play

import UIKit

print("Hello World")

When we run the above program we will get a result like as shown below

 

Hello World

If you observe the above sample code it’s following some basic structure like first comments, import class then required keywords for coding. Now we will see the basic building blocks of Swift programming language.

Comments in Swift

The very first line in the playground project which is highlighted with green text is known as Comments. In all programming languages, the concept of comments remains the same. The Comments are the lines that are not compiled by the compiler and these lines are basically help developers and programmers to read and understand the code in a better way. The Comments are the two types the first type is Single Line Comment and the second line is Multi-Line Comment.

 

The declaration of comments like as shown below

 

Single-line comment

 

//: Playground - noun: a place where people can play

Multi-line comments

 

/* Playground - noun: a

 place where

 people can play */

Swift Import Class

The second line in our playground project is started with an import keyword. The import keyword is the built-in keyword which is used to call or import any class in our project. Suppose if we want to use the functionalities or function of Maps then we need to import the Map class. 

 

Following is the way of importing required functionalities or functions in our project.

 

import UIKit

import MapKit

The importing classes remain same either in Objective C or Swift Programming Language or Cocoa Touch Class.

Semicolons in Swift

In swift programming language Semicolons are not mandatory that means we can easily ignore the Semicolons signs at the end. The compiler will compile the code without having any errors.

 

var str = "Hello, playground"

In case if we want to declare two statements or two variables in a single line then we are restricting to use the semicolon with the first statement or variable after completing, then we will continue the second statement.

 

var str = "Hello"var strr = "Tutlane"

Swift Keywords

Keywords are the identifiers and those are built-in or reserve keywords used by Swift Programming language. In case if we are trying to use keywords as arguments or variable names, then the compiler prompts an error saying that this keyword is the reserved keyword and you don’t have permission to use these keywords.

Built-in or Reserve Keywords

The following are the built-in or reserve keywords available in a swift programming language.

 

Class deinit Enum extension Func import Init
operator private protocol public static struct subscript
break case continue default do else for
return switch where while as false is
dynamicType super true _COLUMN_ Let in _FILE_
internal typealias if nil var self unowned
_FUNCTION_ _LINE_ associativity convenience dynamic didSet precedence
final get infix inout right set type
lazy left mutating none weak willSet prefix
nonmutating optional override postfix Protocol required  

Swift Variables

In swift variables are used to store value in memory and we can manipulate or change the value of variables by applying different operations based on our requirements.

 

We use the keyword called “var” to define variables in swift. Following is the syntax of defining variables in the swift programming language.

 

var<VariableName> = <Value>

Following is the simple example of declaring the variable in swift and assigning value to it.

 

var strr = "Tutlane"

print(strr)

When we execute the above code we will get a result like shown following.

 

Tutlane

Swift Constants

Constant means it refers to the fixed value it means when we declare variable as constant then it’s not possible for us to change or override it throughout the program. Generally, we declare constant variables with keywords called “let”.

 

Following is the syntax of declaring the constant variables.

 

let <ConstantName> = <Value>

Following is the simple example of defining the constant variables in a swift programming language.

 

let strr = "Tutlane"

print(strr)

The above statements will return output like as shown following.

 

Tutlane

These are some of the basic syntaxes that we used frequently in swift programming language.