wednesday, september 9, 2015 csci 351 – mobile applications development

13
Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Upload: rosanna-hensley

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Wednesday, September 9, 2015

CSCI 351 – Mobile Applications Development

Page 2: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – arrays introvar facultyList: [String] = ["Booger", "Twig", "Smith"]facultyList.countfacultyList.isEmptyfacultyList.append("Kalata")facultyList += ["Schemm"]facultyList[0] = "Boger"facultyList[2...3] = ["Chris Smith", "Katie Kalata"]facultyList.insert("Coullard", atIndex:3)let oldguy = facultyList.removeAtIndex(0)let newguy = facultyList[0]let solong = facultyList.removeLast()

Page 3: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – looping through arrays

for item in facultyList{ println(item)}

for (index,value) in enumerate(facultyList){ println("Item \(index) is \(value)")}

Page 4: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – more arrays

// define and initialize an empty array of Intsvar exams = [Int]()exams.append(87) // add an item// exams = [] // now it is empty again

var quizzes = [Int](count:14, repeatedValue:0)

var gpa = Array(count:5, repeatedValue: 2.5)

var items = exams + quizzes

Page 5: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift - dictionaryA dictionary is a container that stores multiple

values of the same type. Each value is associated with a unique key,

which acts as an identifier for that value within the dictionary.

Unlike items in an array, items in a dictionary do not have a specified order.

You use a dictionary when you need to look up values based on their identifier.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

Page 6: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – creating a dictionaryvar deptCodes: Dictionary <String,String> =

["MATH":"Mathematics", "CSCI":"Computer Science", "PYSC":"Psychology"]

// Another way using type inferencevar deptCodes = ["MATH":"Mathematics",

"CSCI":"Computer Science", "PYSC":"Psychology"]

deptCodes["CSCI"]deptCodes.count

Page 7: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift - dictionariesdeptCodes["NURS"] = "Nursing"

if let oldValue = deptCodes.updateValue("Nursery Rhymes", forKey: "NURS") {

println("The old value for NURS was \(oldValue).")}

if let deptName = deptCodes["BIOL"] { println("The name of the department is \

(deptName).")} else { println("That department is not in the

departments dictionary.")}

Page 8: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – deleting a dictionary itemdeptCodes["NURS"] = nil

if let removedValue = deptCodes.removeValueForKey("MATH") {

println("The removed department's name is \(removedValue).")

} else { println("The departments dictionary does

not contain a value for MATH.")}

Page 9: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – looping through a dictionary

for (deptCode, deptName) in deptCodes { println("\(deptCode): \(deptName)")}

for deptCode in deptCodes.keys { println("Department code: \(deptCode)")}

for deptName in deptCodes.values { println("Department name: \(deptName)")}

Page 10: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – the for-in looplet names = ["Boger", "Kalata", "Schemm"]for name in names { println("Hello, \(name)!")}

for num in 0...10 { println("\(num) times 7 is \(num * 7)")}

Page 11: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – other control structures

Swift supports the traditional loops that you are familiar with from C or C++:

for loop while loop do-while loop

Blocks for loops and conditionals must be enclosed

let quiz = 65if quiz > 70 { println("You passed")} else { println("You failed")}

Page 12: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – the switch statement In Swift, breaks are not necessary to

“break out” of each case The default case must be includedlet value = "Bob"switch value { case "Joe": println("Joe is cool") case "Bob" : println("Bob is wacky") case "Sally" : println("We love Sally") default: println ("Nobody was found")}

Page 13: Wednesday, September 9, 2015 CSCI 351 – Mobile Applications Development

Swift – one last dictionary slide...// Create an empty dictionaryvar namesOfIntegers = Dictionary<Int,

String>()

// Add an item to the dictionarynamesOfIntegers[12] = "twelve"

// Empty the dictionarynamesOfIntegers = [:]