Sunday, September 27, 2015

Functions in Swift -- IOS

//Functions is commonly used to group a series of statements together to achieve a specific objective

//To declare a function

func helloSwift(){

    print("hello Swift")

}

//To invoke a function

    helloSwift() // the word "hello swift" will be printed 


Function that Return value-

//To declare a function
 func helloSwift() -> String{

    return "hello Swift"
}

//To invoke a function and catch return in var "result"
var result = helloSwift()

//"result" is hello swift

print(result)

Conditional Statements in Swift -- IOS

If / else Statement-

//Deciding on different path of execution . Decision making is done with if and  else statements --

//Executed if marks greater than or equal to 60

if marks >= 60
{
    print("you score first division")
}
else
{
    print("you not score first division")


}

Chained conditions -
//In case where there are more than two possible paths of execution . you can use the if/else if/else block

var marks = 60

if marks >= 60
{
    print("you score first division")
}
else if marks >= 50
{
    print("you  score second division")

}
else if marks >= 40
{
    print("you  score third division")
}
else
{
    print("you failed")
}

Logical Operators - && and ||

Use && operator to make complex decisions involving two (or more) conditions . The result is "true" only if both conditions evaluate true. 

//output is true 
print(true && true)

//output is false
print(false && true)

//output is false
print(true && false)

//output is false

print(false && false)


The ||  operator works in the same way . The result is true  if either one of the conditions evaluates to true

//output is true 
print(true || true)

//output is true
print(false || true)

//output is true
print(true || false)

//output is false
print(false || false)



Example of using && and ||

func getResult (english: Int, science: Int){

    if(english >= 60 && science >= 60)
    {
    print("Passed both test")
    }
    else if(english >= 60 || science >= 60)
    {
        print("pass at least one test")
    }
    else
    {
    print("fail")
    }

}


//Passed both test
getResult(50, 60)

//Pass at least one test
getResult(60, 70)

//Fail

getResult(8, 8)


Nested if/else statements -

var gender = "M"
var age = 18
var discount: Int;


if gender == "M"{
    //execute if gender is male 
    if age > 18{
        discount = 10 //discount (10) if male is above 18 
    }else{
        discount = //discount (5) if male is below 18 
    }
    
}else{
        //execute if gender is not male 
    if age > 18{
        discount = 20
    }else{
        discount = 15
    }

}



Constant Declaration & Mathematical Operations in Swift -- IOS

In swift, use the "let" Keyword to declare constant. For constant , value cannot be changed after declaration .

let number = 100

//This is not allowed, value cannot be changed after declaration




//similarly for constant, you can explicitly declare the data type
let numbers: Int = 7




Mathematical operations -

For numbers , the usual mathematical rules apply . You can use any of the following operations : *, +, -, /, % on numbers. 

//Division operations 

// Int Divide Int = int 

var x = 1/2
result = x = 0


//Int divide decimal value = decimal value 
var y = 1/2.0
result = y = 0.5


//Operations on String

let string = "hello" + "world"
result=  "helloworld"

let str = "3" + "4"
result = "34"

   Code - Checking for even number
// If the reminder of "evenNumber" dividing my 2 is 0 , "evenNumber is an even number"

Var evenNumber = 8
print (evenNumber % 2)

result =  0






Saturday, September 26, 2015

Variable Declaration in Swift -- IOS

//Variable Declaration -

To declare a variable , the data type of the variable is inferred from the value assigned to the variable .

var str = "Hello, playground"

//change the value of the variable 


str = "Hello  Swift"


//Error --- The "str" variable has a datatype of string , and cannot be re-assinged as an Int , float etc..


//You can also explicitly declare the data type in variable 

var name :String = "Hello , Swift"

var age: Int = 45


commonly used data types include String,  Int,  Float,  Double, Bool.  Sample variable declaration for the different data types - 

var S:String = "Swift"
var W: Bool = true
var I: Int = 67
var F: Float = 13.3
var T: Double = 2.2


You can use x.dynamicType to find out the data type of var "X"

let string = "Hello World!”
let array = [“Hello”, “Swift”]
let dictionary = [“dictionary”: 1]

print(string.dynamicType) // "String"

// Get type name as a string
String(string.dynamicType) // "String"
String(array.dynamicType) // "Array<String>"
String(dictionary.dynamicType) // "Dictionary<String, Int>"

// Get full type as a string
String(reflecting: string.dynamicType) // "Swift.String"
String(reflecting: array.dynamicType) // "Swift.Array<Swift.String>"
String(reflecting: dictionary.dynamicType) // "Swift.Dictionary<Swift.String, Swift.Int>"