Concepts And Fundamentals Of Swift Programming Language

Antroduction

In this article, we are going to study some basic concepts and fundamentals of Swift language, which is very important for the new programmers and developers who change their fields and move towards the iOS app development.

What Is a Variable?

  • Variable is nothing but a place where you want to store your data,  but you have to follow some rules when you store data.

  • Swift language is similar to JavaScript and PHP; because when you need to declare any type of variable, you only need to declare Var keyword.

  • We can change variable value at any point which means that we can assign a value,  but later we change the value.

  • By declaring variable name, Camel case is the best approach to declare a new variable.

  • Declare a simple variable with name myVariableName and assign it integer and float value.

We change Variable Value

  • Yes, if you assign one value to a variable or you want to change the value or assign it in a new value, then you will easily do it.
  • In this image, we declare the variable and assign the value,  but in the second or third line, we change the variable value and the compiler can’t prompt any error.

What Is Type Conversion?

  • Type conversion is a mechanism, in which you can convert your data format into another format.



  • You can see that we declare two variables, where one is an integer and the second is double. We want to convert the datatype of decimal variable, but we see that there is an error prompt because we can’t follow the right method to convert the datatype. Basically, in this step, we will assign the value not to convert the value.



  • In this image, you can see that in the third line, we declare a new variable and then we will give the datatype in which we want to convert the variable and subsequently in the round brackets, we pass the variable name and in the fourth line; we add both the value s, which are now integers.

What Is Constant?

  • Constant is similar to a variable, but in constant; we assign the value one time and we cannot change it throughout the whole program.

  • In Swift language, we declare a constant by using let keyword. In this image, we simply declare a constant keyword and assign value to it.

Constant Values Can’t change

  • Constant value can’t change. It will remain fixed. In this image, you can see, when we assign first value to the constant,  the variable will work fine, but when we declare another value it gives an error.

What are Tuples

  • You can see that some data comes in a pair or triplets form like (x,y) for 2D Grid and (x,y,z) for 3D Grid so, if we want to represent these types of data you use tuples.

  • We can declare the variable and constant tuple as it depends on the requirement.

  • In this image, you see that I declare one tuple with x and y coordinates. You can declare any datatype of tuple, as there is no restriction in Swift language.



  • If we want to access or call x and y coordinates separately, we do this by giving the index value but, if you want to declare a new variable, because you want to make its value in a separate way.



  • If you want to assign the name of each coordinate in a tuple, then you can do this in Swift language very easily. In this image, we declare the tuple name and in round bracket, first we declare our coordinate name, followed by assigning datatype. In the next line, we call both the coordinates.



  • If you want to declare a 3D coordinate, but you learn that the third coordinate is not necessary for us; then you use the _ sign to ignore this coordinate.

This is the end of this article and now I think that you have a better understanding of these types of things. In future, we will discuss some more fundamental concepts of Swift language.

Next Recommended Readings