Learning Swift : Variable and Constants

Hello Everybody….

All iOS applications are written using Swift Language. So in following post series starting from this one, we would learn more on swift basics.

In the last post on Installing Xcode, we installed Xcode 8 on our mac.

Xcode has a playground where we can learn swift language quickly and easily. Before starting on Swift, we need to get acquainted with Playground

Therefore, we will go through the following topics step by step :

  1. Meet Playground
  2. Comments
  3. Constants
  4. Variable

Meet Playground

In this section we will see how to create a playground file and get to look at playground UI minutely.

Creating a Playground File

Open Xcode and Click on “Get Started with a playground”

xcode install complete

Next enter a filename of your choice and click on “next”

Enter file name

Next choose the location where you would like to save the file and click on “create” and “finish” on the subsequent screen.

select location

Your playground file is created and would open like this with some default text.

playground

What’s what in Playground?

play-windows

  1. Navigation Window : This window opens when you click on the first button in the panel shown by arrow. This shows all the files in your playground.
  2. Source Code : Here we write in our swift code.
  3. Result Sidebar : Playground automatically runs and shows the results here. Automatic execution here means, you type in code and you see the results here.
  4. Execution Window : This window opens when we click on the second button in the panel shown by arrow. It has a run button (blue triangle). When clicked, displays the result of swift code. It is used for manual execution.
  5. File and Library Inspector : This window opens when we click on the third button in the panel shown by arrow. File inspector shows the details about the file and library inspector is used to add components from code library to the source code.

You can find the source code for this post at Learning Swift – Variables and Constants

Comments 

Comments are the lines of code which are ignored at the time of execution of code. In swift single line comments are written using “//” and multi-line comments are written within “/* ….. */”

For Example :

//Variable and Constants. This is a single line comment
/* This is a multi-line comment. You can have more than one line. */

Constants

Constants are those values which cannot change during execution.

For Example :

let firstName : String = "John"

Here firstName is the name of the constant.

String is the type of firstName and

John” is the value assigned to firstName.

This whole line is called an expression

Let’s try to change the value of firstName

firstName = "Dave"

Playground throws an error like this :

error

Variables

Variables are those values which can be changed during execution. There will be no error shown.

For example :

var firstName : String = "John"
firstName = "Dave"
print (firstName)

In line 1, value of firstName is “John”. In second line the value changes to “Dave”. When you will run the above code, the output will be “Dave”.

In my next post, we will look at various data types in Swift. Till then bbyee.