Swift Programming - Zero To Hero - Part One

Introduction

This is Part one of Zero to Hero in Swift Programming. This is for beginners those who want to get started into Swift Programming .In this Article we will learn,

  • What is Swift?
  • Why need Swift?
  • Environment for Swift Programming
  • Variables in Swift

What is Swift?

A Swift is a programming Language developed by Apple for developing iOS and OS X Development. Swift is replacement of Objective C programming and it is based on C programming language. The syntax is similar to Objective C.

Swift comes with a Playground feature where Programmers can write code and immediately see the output. The early version of swift was released in 2010. Core ideas of Swift Design are taken from popular programming languages such as Objective C, Rust, C#, Ruby, Haskell, Python and CLU.

Why do we need Swift?

Swift is one of the easiest languages to learn. To develop Native iOS applications, we have to use Swift.  Swift reads quite close to English, so it is rather easy to pick up for beginners. Like other typed languages, Swift is faster than dynamically typed languages because things are more clearly defined.  Apple APIs feel pretty natural in Swift.

Environment for Swift Programming

The IDE used for Swift Programming is xCode.  We can download the setup from Apple store or from apple official website.

Downloading from Apple Store

Enter into Apple store search xCode in the Search bar. Then click Download option.

Downloading from Website

In order to download from a website you have to log in Apple’s site, and then follow the link.

This will lead to a page where you can download the setup based on the system requirements. 

Swift

After installing, open the xCode and click the option “Get Started with a Playground” and the window will be displayed as below. 

Swift

Variables in Swift

We don’t want to call any library files to perform I/O operations.  We can declare variables using var and let. 

Var
Var is used to make a variable.

Example

Var FirstName=”Sundar”

Output 

“Sundar”

The default value determines the variable type. That is we don’t want to mention the type of variable as Int, String, Float like other language. In the above given example, “Sundar” is  a string. So var FirstName is a string type.  Lets see some other var  types,

Example

Var  i=1

Output 1

1 is an integer value, so i is integer (int) type.

Example

Var j=1.253

Output

1.253

1.253 is a float value, so j is float (decimal) type.

We don’t want to give semi colon (;)   at each statement ending.

String Concatenations

Like other programming languages we don’t want to specify the symbols as   “  + + “  to concatenate the string.  

Example

Var FirstName=”Sundar”
Var LastName=”S”
Var FullName=”Hello \ {FirstName} \ {LastName} ”

Output 

Hello Sundar S”  

\ (Lash) is used to concatenate two variables along with {} Parentheses. In the Parentheses we have to pass the variable name.

Strongly Typed Variables

When a type is declared to a variable then it’s a strongly typed variable. For the entire program the variable will be that declared type.

Example

Var FirstName:string =”Sundar”

FirstName will be string type. We cannot declare any other type values  like int, float . Only string values will be hold in FirstName variable

Var  i:int=1

Variable i will hold only integer values.

Arithmetic Operations

We can perform arithmetic operations like Addition, Subtraction, Multiplication and Division using variables.

Example

Var i=100
Var j=8
Var result=(i*j)

Output

800

Constant Variables (let)

let is used to declare constants. Once variables are declared as constant, then for  that entire program the value will be same. It's similar to Static variables in c#.

Example

let i=1000

For the entire application the variable value will be 1000. We cannot reassign it. It will not change during runtime. 

Conclusion

In this article I have shared the basics of Swift programming. In  upcoming articles we will go a little deeper into Swift programming.


Similar Articles