Hello Folks,

Today we are going to understand what is SWIFT Programming Language?

Swift is the programming language use to develop iOS, Mac OS, Apple watch OS applications. It is a functional programming language. Earlier all the apple products application based on Objective-c which is not easy to learn and complex to understand. But we can easily start with swift it is easily understandable. Swift is introduced in mid of 2014 by Chris Lattner. Swift compiler based on LLVM “ Low Level Virtual machine ”, Swift is user friendly and fast language.

What is Swift Programming Language

Advantages of swift over objective-c

  • Swift doesn’t need semicolon (;) to end the statement.
  • Swift doesn’t need aestrik (*) to declare property.
  • Swift introduces “let” and “var” keywords for limited memory allocation.
  • It introduces typecasting.
  • Swift is type inference it automatically take the type of the variable.
  • It introduces the concept of optionals and optional binding to handle the nil value.
  • It introduces guard.
  • It gives the concept of closure.
  • A very useful concept of tuple which hold more than one value.
  • Concept of typealias.
  • We can use the keywords as a property name.

Normal statement

let myConstant = 5

MyConstant = 10 (Give error let statement is immutable)

In above swift statement “let” is keyword define for constant means its value is immutable. To make it mutable we have to define as

var myVariable = 6
myVariable = 10

In the above statement we didn’t define the type of the let and var variable but swift compiler automatically get the type of the variables which is Int.

Optional

We can make variable optional it may contain a value or nil

Example

Var optionalValue: String?

To make a variable optional you need to simply add question mark “?” and it is not compulsory to initialise now it has the nil value.

Tuples

// To store name and age of employee

let tupleExample:[(String, Int)] = [(Alex, 24), (James, 25), (Harriet, 19)]

To access the value

let firstEmployeeName = tupleExample[0].0 // It gives Alex
let firstEmployeeAge = tupleExample[0].1 // It give age 24

TypeAlias

Here is the example of typealias

typealias MyFunctionDefinition = (Integer, String) -> Void

In the the left part is hold by the MyFunctionDefinition

Guard statement:

Guard statement is used to check the optional value and it increases the scope of the variable and else statement is compulsory with the guard statement.ios swift tutorial for beginners

If statement

If myValue != nil {

//do Something

// myValue scope is limited only in this block

}

In the above if statement else statement is up to you compiler did throw any error regarding else state.

Guard statement

var value: String? = “abc”
guard let checkOptionalValue = checkingValue as? String else { return }

Here checkOptionalValue scope is wide and else statement is compulsory with the guard block. Else statement executes if checkingValue contains nil value stop execution after return.

Optional Binding

Optional binding is used to check nil values. It is apply on optional variable only to safely get value.

var optionalValue: String? = “abc”

print(optionalValue)

The output of the above statement on console is
optional(“abc”)

If let value = optionalValue {

//This block execute if optionalValue doesn’t contain nil value

print(value)

The output of the above statement on console is
“abc”

}else {

// This statement executes when optional value is nil

}

Read more: How many steps are in the system development life cycle

Leave a Reply

Your email address will not be published. Required fields are marked *