Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

Swift Tutorial

As all developers related to iOS and mac OS X app development are aware of the fact that Apple launched new programming language to develop IOS and mac OS X apps. This new programming language is named as SWIFT. In this post we will cover basic part of the language.

Here is a list of the other articles in this series already published:

Please stay tuned for updates and new additions by following us on twitter , Liking us on facebook or signing up to our Monthly Mailing List

Constants and Variables

These two terms are self explanatory, however, if you do not know, then the word constant refers to the values which never changed during the life of algorithm or during runtime, whereas variables refers to the values which can be changed during runtime or life of our program. The value of constant cannot be changed once set whereas variable are totally opposite to constant.

Declaring a constant and variable in Swift


let label = "The Width is: "
let width 94

The rule of thumb when programming with the iOS 8 Swift programming language = is that you have to declare variables/objects before using them in your iOS 8 Application. In Swift you can declare a constant using the let keyword and you can declare a Swift variable using the var keyword.

Though its not compulsory in Swift language to terminate each line of code with semicolon, its totally your wish whether to use it or not.

So, Let us take an example, we will create a constant for maximum number of lives a player had in a game and a variable to keep track of the lives used by the player in a game.


let maximumNumberofLives = 5;
var currentLive = 0;

In the above Swift code we have declared our constant as an integer value, as we specify an int to our constant, the same is for variable. You can give different data type values to them. For example giving the float value


let maximumNumberofLives = 5.0;
var currentLive = 0.0;

In Swift, you can also declare multiple constants or multiple variables in a single line. These must be seperated by comma.


var x = 0,y = 0,z = 0;
let x = 0,y = 0,z = 0;

You can provide data type to variable and constants so that you can store or provide values later, commonly known as typeAnnotations. For this use syntax.

Common Data Types in Swift

You will be using these basic types most often when declaring variables:

  • Int is used for whole numbers
  • Float is used for numbers with decimal points
  • Double is used for bigger numbers with decimal points
  • String ordered sequences of characters
  • Bool means true or false

These types have the same meanings as you would expect from other programming languages: Int, Float and Double are all type sof numbers. String is an ordered sequence of characters and Bool presents a true/false condition.

Here’s some examples of how these basic types would be declared and assigned values in Swift:


var intType:Int = 1
var floatType:Float = 2.0
var doubleType:Double = 3.0;
var stringType:String = "ABCDE"
var boolType:Bool = true

Keyword name of Constant: datatype


var welcomeMessage: String

The above example creates a Swift variable with datatype string, colon can be read as “…of Type..”, so you can read above line of code as ‘Declare a variable called message of type string’. Now you can give any value to the message variable:


welcomeMessage = "Welcome to Swift Language";

To print message on console you can use println() function. A basic println() function is Swift is like this:


println("This is a string");

Now, to pass values in println() you can pass the value i.e your variable or constant inside a parenthesis and placing backslash in front of opening parenthesis.


println("The current value of friendlyWelcome is \(welcomeMessage)");

Thank you. Stay tuned for the other articles in the series.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I am a iPhone developer having 3+ years of working experience in developing iPhone/iPad apps.

Comments

comments