Swift Basic Syntax (Comments, Keywords, Semicolons)

  Total Views : 267
  Comments : 0
  Keywords : Swift comments / constants syntaxes with examples, Swift keywords / semicolons syntaxes with examples, swift basic commands with syntaxes

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. 

Swift Basic Syntax

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.

 

Swift Playground Interface to Create Applications

 

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

import UIKit

print("Hello World")

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

 

Hello World

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.

Comments in Swift

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

 

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 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 remains same either in Objective C or Swift Programming Language or Cocoa Touch Class.

Semicolons in Swift

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"

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 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.

Built-in or Reserve Keywords

Following are the built-in or reserve keywords available in 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 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"

print(strr)

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

 

Tutlane

Swift Constants

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"

print(strr)

The above statements will return output like as shown following.

 

Tutlane

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User