Muhammad Harits Syaifulloh

Saturday, September 17, 2016

Swift 3 - Variables in Swift 3

Variables in Swift 3 or variables in any language for that matter. Variables are the fundamental building blocks of anything that you're going to create. Their little boxes if you like, that hold bits of data or lots and lots of complex data. Let's see what they are by playing around with them inside of our playground.

So in our playground we have already got import UIKit and var str that's equal to "Hello, Playground". Now what this is doing is it's declaring a variable and that variable is str. That name of variable can almost anything you like, but my recommendation to you is to name it something sensible. So when you look at your code later on, it make sense and you know what it does just from the name.

We have this other word called var. Var implies this is variable i.e it might change at some point at the future. Then we have an assignment operator which is equals (=). This takes the right hand side and puts it into the left hand-side of the variable. So "Hello, Playground" gets put into str.

There are quotes there because this is a string and a string is essentially a sentence or a phrase or just a collection of characters like numbers and letters. Now what if we wanted to put something else inside str. Well, we have already declared by saying var str so if we wanna put something else in it, all we have to do now is reference it by typing str is equal to what (str = "something") and let's see the result.

At the moment swift is inferring the type. It knows that when we put things inside of the quotes that were actually declaring a string. But sometimes this would be so obvious to Swift, so we have to explicitly say. And the way we explicitly say what type of variable it's is by colon and then typing the type. In this case it's a string so it will look like the following picture :

But what if we wanna make that str or age or answer, etc., unchangeable?. Let's say we are working on a team of programmers and we don't want anyone to change the value of str. We want them to get an error when they even try to do it.

Well we can assign a let constant.

What let's size is we cannot change this constant once we have assigned it at the first time. That's why we get the error while try to assign str with the new value.

In reality we're not going to use let that often but when you start working in teams it's a vary good idea to do that.

Download variables in Swift 3's playground file here

Alright guys that's all about variables in Swift 3 and we've learn a little bit about let too. In the next lesson we'll learn about Collection in Swift 3. So keep update and stay tune on Ask MHS. If there are any questions, just leave some comment and I'll answer it soon and also we can discuss more about this lesson.

Thank you very much, hope this lesson can make us easier on learn Swift 3. See you next time in the next lesson.

1 comment:

  1. Sangat mudah difahami penjelasannya.
    Terimakasih Ask MHS (M Harits S) atas lessonnya (y)