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 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.
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.
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.
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:
Bool in Swift:
String 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.
When we declare any variable in swift, without mentioning datatype. Swift automatically decide the data type of that variable. For example
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.
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.by
7 thoughts on “Swift Programming Language: Data Types”
Hello, I enjoy reading all of your article post. I wanted to write a little comment to support you.
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
I think the admin of this web site is genuinely working hard for his site, as here every data is
quality based material.
my site code free wifi
Hi, I want to subscribe for this website to get newest updates, thus
where can i do it please help out.
Great site you’ve got here.. It’s hard to find good quality writing like yours nowadays.
I seriously appreciate individuals like you! Take care!!
Yes! Finally something about website.