25
iOS Development using Swift SWIFT BASICS (2) Ahmed Ali

iOS development using Swift - Swift Basics (2)

Embed Size (px)

DESCRIPTION

Learn iOS development using Swift (Arabic tutorials) - Second session slides, which covers the following topics: - Data types - Strings - Collection types - Control flow - Demo on Connecting UI Controls to Your Code

Citation preview

Page 1: iOS development using Swift - Swift Basics (2)

iOS Development using Swift

SWIFT BASICS (2)

Ahmed Ali

Page 2: iOS development using Swift - Swift Basics (2)

TODAY TOPICS

• Data types

• Strings

• Mutability, comparison, and interpolation

• Collection types

• Arrays, dictionaries and their mutability

• Control flow

• Connecting UI Controls to Your Code

Ahmed Ali

Page 3: iOS development using Swift - Swift Basics (2)

DATA TYPES

• Integers

• Int

• UInt

• Floating point

• Float

• Double

• Boolean

• Bool

Ahmed Ali

Page 4: iOS development using Swift - Swift Basics (2)

DATA TYPES - TUPLES

• Multi-values in one reference

• You can pass a tuple as a method parameter, return it from a function (or method), make it the type of a reference.

• Definition Syntax:

• var tupleVar : (Int, String)

• Assigning it a value:

• tupleVar = (5, "String value”)

Ahmed Ali

Page 5: iOS development using Swift - Swift Basics (2)

DATA TYPES - TUPLES (CONT)

• Tuples values can be access through their index number or names

• Accessing the value through its zero-based index

let intVal : Int = tupleVar.0 // equal to 5

let strVal : String = tupleVar.1 // equal to "String Value”

• Named tuple elements

let response : (errorCode: Int, errorMessage: String) =

(404, "HTTP not found")

print("Error connecting: \(response.errorMessage) with error code: \(response.errorCode)")

Ahmed Ali

Page 6: iOS development using Swift - Swift Basics (2)

STRINGS

• Syntax:

• let myStr = "String value”

• Mutability (modifiable)

• If the reference is defined with var keyword then it is mutable (can be modified)

• If the reference is defined with let keyword then it is immutable (can not be modified)

• Examples:

//Valid

var myStr = "String value"

myStr += " Additional string"

Ahmed Ali

Page 7: iOS development using Swift - Swift Basics (2)

STRINGS (CONT)

• Mutability invalid example:

//Invalid

let myStr = "String value"

myStr += " Additional string"

Ahmed Ali

Page 8: iOS development using Swift - Swift Basics (2)

STRINGS (CONT)

• Comparison:

• You don’t need to call a special method to compare to string references.

• You can use the == operator is used to compare two string reference and see if they hold the same string value.

• The === can also check against the reference it self, this operator checks if both reference are the same, i.e refers to the same memory address.

Ahmed Ali

Page 9: iOS development using Swift - Swift Basics (2)

STRINGS (CONT)

• Interpolation

• Simple syntax:

let hello = "Hello”

let world = "World”

let symbol = "!“

let helloWorld = "\(hello) \(world) \(symbol)"

Ahmed Ali

Page 10: iOS development using Swift - Swift Basics (2)

COLLECTION TYPES - ARRAYS

• Can hold list of values of the same type.

• Definition syntax:

let arr : Array<String> = ["val 1", "val 2", "etc..."]

//shorthand version

let arr : [String] = ["val 1", "val 2", "etc..."]

//Shorter version using type inference

let arr = ["val 1", "val 2", "etc..."]

Ahmed Ali

Page 11: iOS development using Swift - Swift Basics (2)

COLLECTION TYPES - ARRAYS (CONT)

• Access and modify array content:

//to be modified, it must be a variable

var arr = ["v1", "v2", "v3"]

println(arr[0]) //prints first element

arr[0] = "newV1" //changes first element

println(arr[0])

arr.removeAtIndex(1) //removes second element

arr.append("anotherNewValue")//appends an element

arr += ["v4", "v5"] //append more than one element

Ahmed Ali

Page 12: iOS development using Swift - Swift Basics (2)

COLLECTION TYPES - DICTIONARIES

• Can hold list of key : value pairs.

• Definition syntax:

let dict : Dictionary<String, Int> = ["A" : 15, "B" : 16]

//shorthand version

let dict : [String: Int] = ["A" : 15, "B" : 16]

//Shorter version using type inference

let dict = ["A" : 15, "B" : 16]

Ahmed Ali

Page 13: iOS development using Swift - Swift Basics (2)

COLLECTION TYPES - DICTIONARIES (CONT)

• Access and modify dictionary content:

//to be modified, it must be a variable

var dict : Dictionary<String, Int> = ["A" : 15,

"B" : 16]

println(dict["B"]) //prints 16

dict["B"] = 30 //modifies the value of "B" key

dict["C"] = 19 //adds new key : value pairs to the dictionary

dict.removeValueForKey("A") //removes the key with its value

Ahmed Ali

Page 14: iOS development using Swift - Swift Basics (2)

CONTROL FLOW - IF

• Like any traditional if condition you are familiar with, with additional capabilities.

• Optional binding:

var dict = ["K" : 15, "G" : 16, ”A" : 5]

if let value = dict["A"]{

println(value)

}

Ahmed Ali

Page 15: iOS development using Swift - Swift Basics (2)

CONTROL FLOW - IF (CONT)

• If condition binding variable to a tuple

• We can use optional binding to map tuple values to variables

let params = ["name" : "Ahmed", "pass" : "123456"]

if let (cached, response) = performHttpRequest("login", params){

}

//or if only one value matters

if let (_, response) = performHttpRequest("login", params){

}

Ahmed Ali

Page 16: iOS development using Swift - Swift Basics (2)

CONTROL FLOW – FOR

• Traditional for loop and varieties of for each

• Traditional for loop:

let arr = [1, 2, 3, 4, 5, 6, 7]

//The parentheses here are mandatory in this old style for loop

for (var i = 0; i < arr.count; i++){

}

Ahmed Ali

Page 17: iOS development using Swift - Swift Basics (2)

CONTROL FLOW – FOR EACH

• For each

• Removing the parentheses is mandatory for all varieties of for each loop

• For each with open range operator:

for i in 0 ... 10 {

//this loop will loop from the value 0 to the value 10 (11 loops)

}

Ahmed Ali

Page 18: iOS development using Swift - Swift Basics (2)

CONTROL FLOW – FOR EACH (CONT)

• For each with half open operator:

for i in 0 ..< 10 {

//this loop will loop from the value 0 to the value 9 (10 loops)

}

Ahmed Ali

Page 19: iOS development using Swift - Swift Basics (2)

CONTROL FLOW – FOR EACH (CONT)

• For each with arrays:

let arr = [1, 2, 3, 4, 5, 6, 7]

for element in arr{

}

Ahmed Ali

Page 20: iOS development using Swift - Swift Basics (2)

CONTROL FLOW – WHILE AND DO .. WHILE

• While and do .. While, has nothing different than what you already from Java, C, C++ or PHP.

Ahmed Ali

Page 21: iOS development using Swift - Swift Basics (2)

• No implicit fallthrough.

• No need for break statements.

• Use fallthrough if you want the case to fallthrough.

• Switch cases can be against non-numeric types.

• Switch cases are exhaustive: all possible values must have a matching case.

• Have many varieties to handle range matches, tuple mapping, where conditions, and bindings.

let myConst = 2;

switch myConst{

case 1:

println("myConst = 1")

case 2:

println("myConst = 2")

case 3:

println("myConst = 3")

default:

println("myConst = \(myConst)")

}

CONTROL FLOW - SWITCH

Ahmed Ali

Page 22: iOS development using Swift - Swift Basics (2)

• Range matching example

• Note: all possible numeric values must have a matching case, so a default case is mandatory.

switch myConst{

case 0 ... 4:

println("myConst is between 0 and 4")

case 5 ... 50:

println("myConst is between 5 and 50")

case 51 ..< 60:

println("myConst is between 51 and 59")

default:

println("myConst = \(myConst)")

}

CONTROL FLOW – SWITCH (CONT)

Ahmed Ali

Page 23: iOS development using Swift - Swift Basics (2)

• Fallthrough example

• If the second case is matched, it’ll cause all the cases beneath it to be executed as well.

• switch myConst{

• case 0 ... 4:

• println("myConst is between 0 and 4")

• case 5 ... 50:

• println("myConst is between 5 and 50")

• //if this case matched, it'll pass execution to the all the cases beneath it

• fallthrough

• case 51 ..< 60:

• println("myConst is between 51 and 59")

• default:

• println("myConst = \(myConst)")

• }

CONTROL FLOW – SWITCH (CONT)

Ahmed Ali

Page 24: iOS development using Swift - Swift Basics (2)

• let x where ?

• Binding and where conditions

switch myConst{

case let x where x >= 0 && x < 5:

println(x);

case 6..<9:

println("between 6 and 8")

case 9, 10, 11, 12:

println("myConst = \(myConst)")

default:

println("myConst = \(myConst)")

}

CONTROL FLOW – SWITCH (CONT)

Ahmed Ali

Page 25: iOS development using Swift - Swift Basics (2)

Connecting UI Controls to Your Code