Mobile App Development 2021W Lecture 5

From Soma-notes
Jump to navigation Jump to search

Video

Video from the lecture given on January 25, 2021 is now available.

Notes

Lecture 5
---------
Lecture topics
    Swift functions & closures
    Swift dictionaries & arrays
    Writing simple command line synchronous interface in Swift
    Declaring custom views in SwiftUI
    SwiftUI @State and @Binding
    SwiftUI ForEach

Key ideas
 - functions can be passed around as data
 - functions without names are called closures, in other languages lambda is the term
   (lambda comes from lamda calculus, worth looking up)
   
Why closure?
 - encapsulates the lexical scope of where the function is declared
 - regular languages do (mostly) lexical scoping)
   - variables & other symbols are valid or not depending on where they are found
     in the source code
 - lexical scoping really only comes up when you have functions defined inside of
   other things (like struct, class, function)
    - closures "remember" where they were declared, can see those symbols

closures are typically used for callbacks
 - you pass a function in as an argument, it will get called later by someone else
   at some time, you don't know when or in what context

Event driven programming tends to lead to closures in some form
 - because you have to specify what happens in response to events