wednesday, september 9, 2015 csci 351 – mobile applications development
TRANSCRIPT
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()
Swift – looping through arrays
for item in facultyList{ println(item)}
for (index,value) in enumerate(facultyList){ println("Item \(index) is \(value)")}
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
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
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
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.")}
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.")}
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)")}
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)")}
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")}
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")}
Swift – one last dictionary slide...// Create an empty dictionaryvar namesOfIntegers = Dictionary<Int,
String>()
// Add an item to the dictionarynamesOfIntegers[12] = "twelve"
// Empty the dictionarynamesOfIntegers = [:]