IOS final 3 (swift)

  1. how to print Hello, World!
    print ("Hello, World!")
  2. var str = "Hello, playground" 
    str = "Hello, Swift" 
    let constStr = str 
    constStr = "Hello, World!"
    what are the outputs? errors?
    • "Hello, playground"
    • "Hello, Swfit"
    • "Hello, Swift"
    • cannot assing to value: 'consStr' is a 'let' constant
  3. what would be the types for nextYear, bodyTemp
    • var nextYear: Int
    • var bodyTemp: Float
    • var hasPet: Bool
  4. what are swift's basic types and what ar ethey call?
    • they are call structures, not classes 
    • • Numbers: Int, Float, Double
    • • Boolean: Bool
    • • Text: String, Character
  5. What are collections:
    • • Array<T>
    • • Dictionary<K:Hashable, V>
    • • Set<T:Hashable>
  6. write an array of ints with elements 5 and 10 in normal, shortcut and minimally
    • • Arrays are written like this:
    • var arrayOfInts: Array<Int> = [5, 10]
    • • Or you can do the shortcut:
    • var arrayOfInts: [Int] = [5, 10]
    • • Or minimally
    • var arrayOfInts = [5, 10]
  7. create: 
    someInts as an empty array of Int 
    now conatins one value of type Int 3 
    empty again but still type [Int]
    var someInts = [Int]()

    someInts.append(3)

    someInts = []
  8. countingUp is an immutable array of String ["one","two"]

    create immutable secondElement that holds: "two"
    code that tells you number of elements
    • let countingUp=["one", "two"]
    • let secondElement=countingUp[1]
    • countingUp.count
  9. countingUp is an mutable array of String ["one","two"]
    create immutable secondElement that holds: "two"
    code that tells you number of elements
    add to countingUp the element "three"
    code to show number of elements in countingUp.
    • var countingUp=["one", "two"]
    • let secondElement=countingUp[1]
    • countingUp.count

    • countingUp.append("three")
    • countingUp.count
  10. create mutable numbers array 76 75 5 89 45 
    create sum with value 0 
    create for loop that sums all numbers 
    print sum
    • var numbers = [76,75,5,89,45]
    • var sum=0
    • for i in 0..<numbers.count { sum+= numbers[i] }
    • print (sum)
  11. code a for loop with its shortcut 

    array name is numbers and sum is variable being printed at the end.
    • for i in 0..<numbers.count { sum+= numbers[i] }
    • print (sum)

    • for n in numbers { sum += n }
    • print (sum)
  12. how to give a nil default value or option to to reading 1
    var reading1: Float?
  13. if you have: 
    reading1 = 9
    reading2 = 8
    reading3 = 7 

    how would you unwrap to get the avgReading and print it?
    use if-let statement 

    • if let r1 = reading1, let r2 = readin2, let r3 = reading3 { let avgReading = (r1 + r2 + r3)/3 print (avgReading)
    • }
  14. create empty dicitionary namesOfIntegers that can hold an integer and a String

    2. now make it hold 1 key value pair 16 "sixteen" 

    3. make it empty again
    var namesOfIntegers = [Int: String]()

    namesOfIntegers[16] = "sixteen"

    namesOfIntegers = [:]
  15. create dicitionary namesOfIntegers that can hold an integer and a String with values 
    16 "Sixteen" and 5 "Five" 

    Do the normal, shorthand and minimally
    • var namesOfIntegers: Dictionary<Int,String> = [16:
    • "Sixteen", 5:"Five"]
    • • Or shorthand
    • var namesOfIntegers:[Int:String] = [16: "Sixteen",
    • 5:"Five"]
    • • Or minimally
    • var namesOfIntegers = [16:"Sixteen", 5: "Five"]
  16. create a dictionary of prices with string and integer with values 
    "Apples" 4 , "Milk" 2, "Chocolate" 5 

    and print the prices

    now add "Nuts" 6 and print again and show outcome

    now update Milk with 5. and print and outcome
    var prices=["Apples": 4, "Milk": 2, "Chocolate": 5] 

    print(prices)

    ["Chocolate": 5, "Milk": 2, "Apples": 4]

    ["Apples": 4, "Nuts": 6, "Milk": 2, "Chocolate": 5]

    ["Apples": 4, "Nuts": 6, "Milk": 5, "Chocolate": 5]
  17. create a loop for Dicitonary prices with value food and price where Item and $ go vefore values.
    • for(food, price) in prices{
    • print("Item /(food): $\(price)")
    • }
  18. what is an optional?
    • You can change the value of an element - if you want a nil default —to an
    • optional by adding a ?. An optional contains a value or nil.
  19. to unwrap an optional with an exclamation point use _______

    but if the value inside is null it: ______
    !

    will create a fatal error

    the program will crash when the value inside is nil.
  20. difference between dictionary and arrays
    subscripting an array beyond its bounds causes a runtime error, 

    results of subscripting a dictionary are optional
  21. what is string interpolation? 

    create for loop nameByParkingSpace with values space and name and create string interpolation permit that would print space and name 

    example Space 27: Bob
    evaluating expressions enclosed within \(and) and inserting them into the string at runtime. (otherwise you would have to convert everything to string one by one)  

    • for (space, name) in nameByParkingSpace {
    • let permit = “Space \(space): \(name)”
    • }
  22. var teams=["Cubs": "Go Cubs Go", 
    "Indians": "Boo"]
    print(teams["Cubs"]!)

    how would you simplify with if let
    • if let team=teams["Cubs"]{
    •           print (team)
    • }
  23. create a funcion call max with num1 and num2 doubles and if num1 is greater than num2 then return num1 or else return num2 

    outside put inside of the function max values 80 and 90
    • func max(num1:Double, num2:Double)->Double {
    • if num1>num2 {
    • return num1
    • }
    • else{
    • return num2;
    • }
    • }
    • print("Max: \(max(80, 90))")
  24. initializing as a constructor: 
    create a class shape with variable side that is a double and initialize it with 0.0
    create a function getArea that will return the value side 

    outside of the class create blob that will be equal the class shape
    now print block calling getArea
    • class Shape{
    • var side:Double
    • init(){side=0.0}
    • func getArea()->Double{
    •          return side
    •      }
    • }

    • var blob = Shape()
    • print(blob.getArea())
  25. if you have 

    var side:Double 
    init(){side=0.0}

    if you know the value already then:
    var side=0.0
  26. class Shape{
    var side=0.0

    func getArea()->Double{
             return side
         }
    }

    create a super class square that will initialize side as double and inside it will call original init and side. 

    override getArea to multiply side by it and create function change that takes side and gives the value side the passing value
    • class Shape{
    • var side=0.0

    • func getArea()->Double{
    •          return side
    •      }
    • }

    • class Square:Shape{
    • init(side:Double){
    •    super.init()
    •    self.side = side
    •        }

    • override func getArea() -> Double[
    •    return side*side
    •         }

    • func change(side:Double){
    •     self.side=side
    •        }

    }
  27. class Shape{
    var side=0.0

    func getArea()->Double{
             return side
         }
    }

    class Square:Shape{
    init(side:Double){
       super.init()
       self.side = side
           }

    override func getArea() -> Double[
       return side*side
            }

    func change(side:Double){
        self.side=side
           }

    }

    imagine variable box calling superclass with value 5 and printing box calling getArea 

    code it and show the outcome 

    now what if vox calls change with value 4 and prints box calling getArea
    • var box = Square(side:5)
    • print(box.getArea())
    • 25.0

    • box.change(side:4)
    • print(box.getArea())
    • 16.0
Author
lcsanc14
ID
344066
Card Set
IOS final 3 (swift)
Description
ios test final
Updated