Swift Variables

Image_1

Basic Explanation


Variables are a nothing but tags to something that can hold a value. Say you have a few buckets and you start filling them with fruit. You can visually see the buckets and the fruit in them so you know which one is which. You can label each of the buckets with the name of the fruit and then you do not have to rely on it visually. You can refer to it by the tag name.

In Math, you have definitely come across the same in Algebra, you have seen equations like 4x + 2 = 10 and you solve it to find the value of x.

Similarly in terms of computing, a variable is a memory location that can hold a value of some type. When using it, you do not have to know where in the memory does that value reside as you can simply refer to it by the tag name.

In Swift you can create variables by using the var or the let keyword prefixed to the variable name.

 var name = "Learn Swift Blog"
 var year = 2015
 var posts = 2
 var version = 1.5

Swift assigns a type to each of the variable depending on the type of value, for example name has a text value and hence assigns the variable of type String with year and posts, the values are integer numbers and hence the value type is Int. Where as the version is not an integer and Swift assigns it a value of type Float. This is implicit assigning. The types were not specified and are determined by the compiler and assigned accordingly.

The same could have also been assigned Explicitly, i.e. you can specify the type of variables and not leave it to the compiler to decide and assign. The main reason for you doing so is say you wanted the variable posts to contain decimal values not integer, due to the implicit assignment of posts as Int, if you then try the command posts = 2.5 the compiler will complain and throw an error.

The code above could be explicitly assigned as

 var name:String = "Learn Swift Blog"
 var year:Int = 2015
 var posts:Float = 2
 var version:Float = 1.5

We mentioned earlier that variables can be assigned/declared using the var or the let keywords. We have used var in the samples above, what is let used for? At the start of computing with languages like Basic, let was used to define and assign values to a variable. With languages like C, JavaScript and others, the keyword var is used to define variables. However in Swift there is a clear difference, any variable that is defined with let is treated as a constant. This means that the value once assigned cannot be changed for that variable. Consider the code

 let name = "Learn Swift Blog"
 var year: Int = 2015
 var posts: Float = 2
 var version: Float = 1.5

 name = "Just Kidding"  // This will throw an error as the variable name is a constant
 version = 1.7
 posts = 3.2

You can see that the value of the variable name cannot be altered as it was declared with a let, however the other values of version and posts can be altered.

Summary


The syntax for declaring a variable in Swift are as follows (you can choose any one of the options)
var variableName = value
var variableName:variableType = value
var variableName:variableType
let variableName = value
let variableName:variableType = value

Right, that brings us to the end of the intro to variables in Swift. It is a rather easy concept. In the following posts we will have a look at other types and features of Swift. You can follow us on twitter to be notified when a new article is posted.

Views All Time
Views All Time
1726
Views Today
Views Today
1
Posted in Uncategorized and tagged , .