Swift Programming Language: Data Types

Ok Guys. Now we are starting our Second step, that is Data type.

First boring definition but require here:

“In computer science and computer programming, a data type or simply type is a classification, identifying one of various types of data such as real, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.” (Wikipedia)

I think data type is a very basic and very important concept. So I am skipping this part but I will try to explain it in Basic Programming Concepts course.

Now According to our Course content, our next topic is “constants and variables”.

Constants and Variables:

Constants are quantities whose values do not change during program execution. For example I am using PI value in a program. I know that value will not change. So I define it as a constant. Benefit of defining PI as constant is, if someone or myself accidentally try to change the value of PI. Compiler or Interpreter will give me Exception “Hey are you mad. First you are saying it is a constant and after that you are trying to change the value of that constant” 🙂 . Now in swift if we want to make a PI constant, we use the syntax which is shown in below image.

Constants

So “let keyword is used for Constants. One more good thing, we can choose any unicode character for name, like in above image, I use PI sign for name. Also I use Panda character to save a constant string of panda. To use special character, go to Edit-> Special Characters.

Variables are those quantities which are not constant. [ Simple 🙂 ]

For example I create a shooting game. I want to save remaining bullets. Now I will use variable because my value will change when player press a fire button. Now in swift if we want to make a bullet variable, we use the syntax which is shown in below image.

var” keyword is used to define a variable in Swift.

Reference and Value Types:

In computer, when we want to save anything, in memory perspective we have two options.

First is “value type”. In value type, each instance keeps a unique copy of its data.  Second is “reference type”. In this, each instance shares a single copy of the data.

Reference and value types are also a very basic and very important concept. So I will try to explain in Basic Programming Concepts course tutorial.

Number, Boolean and Strings:

OK Guys now I am giving you a very amazing information specially for those who has some background with languages in which you have Primitive types. In swift there is no Primitive type. In Swift we have only Value type or Reference type. So Number, Boolean and String are Value types in Swift. There are some more value types, which I will explain in next section.

Numbers in Swift:

Numbers in Swift

Bool in Swift:

Bool in swift

String in Swift:

Strings in swift

Again String is a Value type object.

Here one important thing, in Swift if we want to insert some variable or constant value into string, we use string interpolation. As show below

var total:Int  = 90

var response:String = “Your total marks are \(total)”

Here we use \(total) as a place holder. So this place holder replaced with our total value.

Type Inference:

When we declare any variable in swift, without mentioning datatype. Swift automatically decide the data type of that variable. For example

String Typeinference

Here name is a String variable. Because Swift is a type safe or strongly type language. So we have no option to change some string variable into other data type after declaration. But question is, if we want to declare some variable not initialise with value, what happen?

As you see in above image Swift saying “Hey tell me its datatype”.

So, to handle above issue, we need to explicitly tell to Swift, that variable I will use as Int. As showing in below image.

Implicit Conversion:

Like many other languages, when we use some binary operator on one int and one float variable. We got answer in float. But in Swift, we have no option like this. If we try we got an error like shown below.

So, to handle this issue we need to convert our variables explicitly before any operation. Like shown below

We have different methods to convert values like String(value), Double(value), Float(value), Int(value) etc.

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

7 thoughts on “Swift Programming Language: Data Types

  1. Excellent beat ! I wish to apprentice at the same time as you amend your web site, how can i subscribe for
    a blog web site? The account helped me a acceptable deal.
    I were tiny bit acquainted of this your broadcast provided vivid transparent concept

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.