Mobile App Development 2021W Lecture 13

From Soma-notes

Video

Video from the lecture given on March 1, 2021 is now available.

Notes

Midterm Review
--------------
* assignment 2 solutions
* answer questions
* go through assignments/tutorials, extract key concepts


Key concepts
 - optional types: if let, !, ??
 - event handlers in SwiftUI for gestures, taps
    - onChanged vs onEnded
 - ForEach in views
   - to make multiple buttons, etc based on a data structure
 - forEach in regular functions
   - how like and unlike for loops
 - positions for SwiftUI images, other objects
    - being able to change them directly, rather than having auto
      positioning/layout
 - @State and @Binding in views, how they connect views
 - state in swiftui, how it interacts with view refreshes
   - events update state
   - changes in state cause views to be redrawn automatically
   - should be able to do some basic problem solving with state & bindings
     - understand what $ is used for in swiftui
 - splitting apart views using @State & @Binding
 - + operator in views, HStack, VStack, ZStack
   - ways to combine views into one view
 - Menu views
 - functions & closures in swift
   - how arguments are specified
   - named vs unnamed
   - return values
 - dictionary, arrays in swift
 - TextField vs. Text
   - input and output on screen
   - bold, italic text
 - Image views
   - using assets included in app
 - for, while loops in swift
   - range specifications with for
 - implicit types
   - when you do and don't have to specify a type
     (basics)
   - the idea is to make code clear without being verbose
 - var vs let in swift
   - variable vs constant values
 - struct vs class
   - value vs reference types
 - string interpolation
   - how to generate a string that includes values in in from variables
      - put a number in a string

On the midterm
 - more reading code than writing code
   - written code will be very short
 - short answer about specific problems and more general concepts
   - general follows the specific
   
What is a foreach-type loop?
 - give it an array-ish thing (iterable) plus a closure which takes one argument
    - closure is called for each element of array

ForEach in SwiftUI is a weird beast
 - like everything in SwiftUI

In swift, like most strongly-typed languages, symbols have types
  - types on a symbol don't change in a scope
    - but you can redefine variables in different scopes, shadowing the old
      definition
    - i in function A and in function B aren't necessarily the same time,
      could be a Double in one, int in another
  - in traditional strongly-typed languages, every symbol had to
    have an explicit type
  - in Swift and Kotlin, types don't have to be specified if they are
    otherwise clear, e.g., you assign a value of a given type

 - if the value of a symbol can change, it is a "variable",
   if it can't, then it is a "constant"
     - var vs let in swift

Separate issue is value types vs reference types
 - this is about what assignment does, A = B
 - with a value type, A = B makes a copy
 - with a reference type, A = B means B refers to the same thing A did

Swift is strongly typed, but it is also implicitly typed
 - so you get the brevity of loosly/dynamically typed languages like
   Python, JavaScript in that you don't have to say the type of everything
 - but you still have the compile-time checking of Java etc

In fact, with optional types, the compile time checking is stricter in
Swift than Java
 - can't "accidentally" do things with a null/nil value

Strongly typed
 - the compiler knows the types of everything at compile time
Loosly typed/dynamically typed
 - compiler doesn't know all types at compile time, so must be checked at runtime

What is a view?
 - struct with a body that is a view

Please answer in complete sentences where you can
 - it is easier to understand and grade, hence better for getting points

But we won't automatically take points off for bullet points

Midterm is DURING CLASS ON WEDNESDAY

Midterm is open book, open note, open internet
 - NO COLLABORATION
 - remember, I'm doing randomized/selected interviews,
   so misrepresenting what you know is a bad idea
 - go ahead and use Xcode
 - I suggest answering everything you can first without using outside
  resources, THEN go back and use online resources/Xcode to refine answers
    - outside resources can take up a lot of time, easy to get
      sucked into figuring out one thing and ignoring the rest of
      the test
 - "open book" isn't so helpful in practice

You have 80 minutes
 - not sure about # questions yet
 - I have a draft exam, but I'm still adjusting it

PMC accomodations
 - go ahead and take your extra time
 - just send me an email saying that you are
   - I have the requests


Midterm will be posted to cuLearn, will submit as a standard "assignment"
 - not using the cuLearn exam mechanisms
 - you just edit and submit a textfile template, same as everything else
 - exam itself will be a PDF
   - so you can print if you like
 - exam will become available at 11:35 AM on Wednesday, should
   be submitted by 12:55 PM
   - will make minor allowances for technical glitches

Don't be in zoom by default
 - msg me on Teams if you have questions, or jump on zoom
 - we can talk via zoom or teams if I can't respond via messaging quickly
   or you can't ask quickly


forEach
 - method of an array

animals is an array

animals.forEach(closure/function that takes one arg)