Here we will learn swift basic syntaxes (constants, comments, keywords, semicolons, variables) with examples and how to use swift constants, variables, semicolons, import statements with examples.
Generally the swift programming playground editor will contain two portions like as shown below. The left portion of editor is to write the code and right side portion is used to see the result of coding.
If you observe above playground editor by default it contains small piece of code like as shown below.
//: playground - noun: a place where people can play
When we run above program we will get result like as shown below
If you observe 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.
The very first line in playground project which is highlighted with green text is known as Comments. In all programming languages, the concept of comments remains same. The Comments are those line which are not compiled by compiler these lines are basically help for developers and programmers to read and understand the code in 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
//: Playground - noun: a place where people can play
/* Playground - noun: a
people can play */
The second line in our playground project is started with 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.
The importing classes remains same either in Objective C or Swift Programming Language or Cocoa Touch Class.
In swift programming language Semicolons are not a 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 single line then we are restricting to use the semicolon with first statement or variable after completing, then we will continue the second statement.
var str = "Hello"; var strr = "Tutlane"
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 an arguments or variable name, then the compiler prompt an error saying that this keyword is the reserve keyword and you don’t have permission to use these keywords.
Following are the built-in or reserve keywords available in swift programming language.
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 keyword called “var” to define variables in swift. Following is the syntax of defining variable in swift programming language.
var<VariableName> = <Value>
Following is the simple example of declaring variable in swift and assigning value to it.
var strr = "Tutlane"
When we execute above code we will get result like as shown following.
Constant means it refers 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 constant variable.
let <ConstantName> = <Value>
Following is the simple example of defining constant variable in swift programming language.
let strr = "Tutlane"
The above statements will return output like as shown following.
These are the some of basic syntaxes which we used frequently in swift programming language.