ios development using swift - swift basics (2)

Download iOS development using Swift - Swift Basics (2)

Post on 27-Jun-2015

596 views

Category:

Technology

2 download

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

TRANSCRIPT

  • 1. SWIFT BASICS (2)iOS Development using SwiftAhmed Ali

2. TODAY TOPICS Data types Strings Mutability, comparison, and interpolation Collection types Arrays, dictionaries and their mutability Control flow Connecting UI Controls to Your CodeAhmed Ali 3. DATA TYPES Integers Int UInt Floating point Float Double Boolean BoolAhmed Ali 4. DATA TYPES - TUPLES Multi-values in one reference You can pass a tuple as a method parameter, return it from a function (or method), makeit the type of a reference. Definition Syntax: var tupleVar : (Int, String) Assigning it a value: tupleVar = (5, "String value)Ahmed Ali 5. DATA TYPES - TUPLES (CONT) Tuples values can be access through their index number or names Accessing the value through its zero-based indexlet intVal : Int = tupleVar.0 // equal to 5let strVal : String = tupleVar.1 // equal to "String Value Named tuple elementslet response : (errorCode: Int, errorMessage: String) =(404, "HTTP not found")print("Error connecting: (response.errorMessage) with error code:(response.errorCode)")Ahmed Ali 6. 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://Validvar myStr = "String value"myStr += " Additional string"Ahmed Ali 7. STRINGS (CONT) Mutability invalid example://Invalidlet myStr = "String value"myStr += " Additional string"Ahmed Ali 8. STRINGS (CONT) Comparison: You dont 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 theyhold the same string value. The === can also check against the reference it self, this operator checks if bothreference are the same, i.e refers to the same memory address.Ahmed Ali 9. STRINGS (CONT) Interpolation Simple syntax:let hello = "Hellolet world = "Worldlet symbol = "!let helloWorld = "(hello) (world) (symbol)"Ahmed Ali 10. COLLECTION TYPES - ARRAYS Can hold list of values of the same type. Definition syntax:let arr : Array = ["val 1", "val 2", "etc..."]//shorthand versionlet arr : [String] = ["val 1", "val 2", "etc..."]//Shorter version using type inferencelet arr = ["val 1", "val 2", "etc..."]Ahmed Ali 11. COLLECTION TYPES - ARRAYS (CONT) Access and modify array content://to be modified, it must be a variablevar arr = ["v1", "v2", "v3"]println(arr[0]) //prints first elementarr[0] = "newV1" //changes first elementprintln(arr[0])arr.removeAtIndex(1) //removes second elementarr.append("anotherNewValue")//appends an elementarr += ["v4", "v5"] //append more than one elementAhmed Ali 12. COLLECTION TYPES - DICTIONARIES Can hold list of key : value pairs. Definition syntax:let dict : Dictionary = ["A" : 15,"B" : 16]//shorthand versionlet dict : [String: Int] = ["A" : 15, "B" : 16]//Shorter version using type inferencelet dict = ["A" : 15, "B" : 16]Ahmed Ali 13. COLLECTION TYPES - DICTIONARIES (CONT) Access and modify dictionary content://to be modified, it must be a variablevar dict : Dictionary = ["A" : 15,"B" : 16]println(dict["B"]) //prints 16dict["B"] = 30 //modifies the value of "B" keydict["C"] = 19 //adds new key : value pairs to the dictionarydict.removeValueForKey("A") //removes the key with its valueAhmed Ali 14. 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 15. CONTROL FLOW - IF (CONT) If condition binding variable to a tuple We can use optional binding to map tuple values to variableslet params = ["name" : "Ahmed", "pass" : "123456"]if let (cached, response) = performHttpRequest("login", params){}//or if only one value mattersif let (_, response) = performHttpRequest("login", params){}Ahmed Ali 16. 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 loopfor (var i = 0; i < arr.count; i++){}Ahmed Ali 17. 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 18. 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 19. CONTROL FLOW FOR EACH (CONT) For each with arrays:let arr = [1, 2, 3, 4, 5, 6, 7]for element in arr{}Ahmed Ali 20. CONTROL FLOW WHILE AND DO .. WHILE While and do .. While, has nothing different than what you already from Java, C, C++ orPHP.Ahmed Ali 21. No implicit fallthrough. No need for break statements. Use fallthrough if you want thecase to fallthrough. Switch cases can be against non-numerictypes. Switch cases are exhaustive: allpossible values must have a matchingcase. Have many varieties to handle rangematches, tuple mapping, whereconditions, 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 - SWITCHAhmed Ali 22. CONTROL FLOW SWITCH (CONT) Range matching example Note: all possible numeric values musthave a matching case, so a defaultcase is mandatory.switch myConst{case 0 ... 4:println("myConst is between 0 and4")case 5 ... 50:println("myConst is between 5 and50")case 51 ..< 60:println("myConst is between 51 and59")default:println("myConst = (myConst)")}Ahmed Ali 23. CONTROL FLOW SWITCH (CONT) Fallthrough example If the second case is matched, itllcause all the cases beneath it to beexecuted 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 tothe all the cases beneath it fallthrough case 51 ..< 60: println("myConst is between 51 and 59") default: println("myConst = (myConst)") }Ahmed Ali 24. CONTROL FLOW SWITCH (CONT) let x where ? Binding and where conditionsswitch myConst{case let x where x >= 0 && x < 5:println(x);case 6..