Swift 2.0 for programmers Part#1

Key points about Swift programming language:

  • Swift is not a superset of C or C++. Whereas, Objective-C was a superset of C.
  • Swift statements do not require semicolon. The compiler won’t complain if you use one, but it is recommended not to.
  • There is no main method/function in Swift. Code executes from top and works on its way down (like Python)
  • You can and should use playground, to quickly test your newly learned Swift code and then add the code chunk to your main project.


Use var to declare variable in Swift

var aVariable = 5
var aString = "Hello World"
var aBool = true 

Points to be noted:

  • Swift is a stronly typed language.
  • It inferres the type of the variable from initial value.
  • Once defined a type of a variable, you can not change it to another type.

if we do not want to provide initial value, we need to tell the type explicitely.

var anIntVariable : Int 		// anIntVariable is of type Int 
var aStringVariable : String 
var aFloatVariable : Float 		// 32-bit floating point 
var aDoubleVariable : Double 	// 64-bit floating point 
var aCharVariable : Character
var aBoolVariable : Bool 

if we want to mention the type as well a value

var aStringVariable : String = "Hello World" // although we don't need to tell the type as we are passing an initial value 


Use let to declare a constant

let aConstant = "Friday"

Constant varaible can be set only once. Although, You can declare it in one place and set the value later.
However, Playgournd does not support the feature yet. that means, you have to set constant value right away in playground.
that also means, you can declare a constant and set it value later in project.

Using constants is encouranged in Swift language and it is important. It is considered best practice to use constant wherever possible. The reasons are,

  1. It is a safety feature.
  2. There are some compiler level optimation and performance improvements if you use constant, instead of varaible.


You will be surprised, how much we are tempted to use var keyword but actually change the value of the variable in its lifetime. We could use let instead.


print("Hello World")

var anIntVariable = 5

print("The value is \(anIntVariable) Great!")	// The value is 5 Great!


var aDouble = 3.5
var anInt = 5

var result = aDouble * anInt 			// compile error.


Swift does NOT implicitly convert values.
anInt variable will not be casted to Float variable.

var result2 = Int (aDouble) * anInt 	// OR,
var result3 = aDouble * Double (anInt)

If statements

if a > 5 {

if b == 0 {
} else {

if a > 5 && b == 0 {

if a > 5 || b == 0 {

Points to be noted

  • Parenthesis is not required around the condition, however, you can put them
  • Curly braces is MANDATORY is Swift, even if there is only one line to execute inside if
  • Conditions must be boolean true or false. Integer variable 0 will NOT be considered false.

Switch statements

let anInt = 10

switch anInt {
case 0:
case 1:

// switch statement with range 

switch anInt {
case 0...3:			// closed range operator (inclusive)
case 4...5:

Points to be noted

  • switch statements must be exhaustive. You have to define all possible cases inside switch. of course you can use default as a fallback of missing cases
  • Fall through does not supported by Swift. You have to have at least one statement inside each case. If there is none then use break


for var i = 0; i < 10; i++ {

for i in 1...10 {       // closed range operator (inclusive)

for i in 0..<10 {       // half-open range operator (last exclusive)

var a = 5

while a > 5 {


repeat {            // same as do-while in other languages
} while a > 5

Points to be noted

  • Curly braces is MANDATORY is Swift, even if there is only one line to execute inside loop


func aFunction () {
	print("Hello World")

// to call 

func aFunction(name: String, id: Int) {

// to call 
aFunction("Hello", id: 1)

func sum(firstNumber : Int, secondNumber : Int) -> Int {
    return firstNumber + secondNumber

// to call 
var res = sum(5, secondNumber: 3)

// default parameters 
func dataFunc(firstName : String = "John", lastName: String = "Doe", id: Int = 0) -> String {
    return "First Name: \(firstName), Last name: \(lastName) Id: \(id)"

// to call 
dataFunc("James", lastName: "Bond")
dataFunc(id: 5)

Points to be noted

  • Function input parameters are by default Constants!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s