Mobile App Development 2022W Lecture 2

From Soma-notes

Video

Video from the lecture given on January 14, 2022 is now available:

Video is also available through Brightspace (Resources->Zoom Meetings (Recordings, etc.)->Cloud Recordings tab). Note that here you'll also see chat messages.


Notes

Lecture 2
---------

Topics
 - participation & tutorial grading
 - structure of classes each week

var vs let
 - var is for declaring variables (i.e., symbols whose value can change)
 - let is for associating values with a name (the associated value never changes)

Notice that names assigned with let are immutable (unlike variables)

In math, x = x + 1 makes no sense
But in programming we often do things like this
 - with var, you can do x=x+1
 - with let, you can't

One oddity in swift
 - you can have var's that are actually immutable (because
   they are part of a larger thing that is immutable)

Note that readLine() returns something of type "String?"
 - so it could be a String, or it could be nil

In C, if you return a string, it is normally a pointer so could be NULL
Java, same thing basically (even though it will be a reference not a pointer)

When you add a ? at the end of a type in Swift, it means it is either that value or nil
 - thus, if you don't have the ?, you are *guaranteed* to have something
   of that type, it will never be nil

Basically, the compiler makes sure that you never treat nil as real data by accident (you have to do it on purpose, and it doesn't make it easy)

"Unwrapping" is the process of taking a value that could be nil and deciding whether or not it is and acting accordingly


Type inference
 - you often don't specify types, but they are always there in Swift
 - moreover, the type of a symbol doesn't change
   - if you try to change it, you'll get an error

"Lexical scoping"

To learn all about Swift, see https://docs.swift.org/swift-book/
 - helpful, but I'll try to cover the key concepts in class

You can use the ?? operator to unwrap a value instead of "if let"
 - you specify a default value after ?? which is used if the value is nil

In SwiftUI, you don't control when the screen is updated
 - instead, your Views specify what the screen should look like at any time
 - SwiftUI will automatically call your code to update the screen
   when relevant data changes (e.g., when @State variables change)

Standard GUI interface
 1. user event happens
 2. program code processes event, updates program state
 3. program updates interface

In SwiftUI, #3 happens automatically
In older GUI frameworks, you have to do this manually
  (we'll be doing it manually for Android)
  (it is also manual for iOS Storyboard/UIKit)