<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Mobile_App_Development_2022W_Lecture_5</id>
	<title>Mobile App Development 2022W Lecture 5 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Mobile_App_Development_2022W_Lecture_5"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_App_Development_2022W_Lecture_5&amp;action=history"/>
	<updated>2026-04-06T01:53:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_App_Development_2022W_Lecture_5&amp;diff=23714&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture given on January 26, 2022 is now available: * [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/lectures/comp1601-2022w-lec05-20220126.m4v...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_App_Development_2022W_Lecture_5&amp;diff=23714&amp;oldid=prev"/>
		<updated>2022-01-26T21:20:47Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture given on January 26, 2022 is now available: * [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/lectures/comp1601-2022w-lec05-20220126.m4v...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture given on January 26, 2022 is now available:&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/lectures/comp1601-2022w-lec05-20220126.m4v video]&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/lectures/comp1601-2022w-lec05-20220126.cc.vtt auto-generated captions]&lt;br /&gt;
Video is also available through Brightspace (Resources-&amp;gt;Zoom Meetings (Recordings, etc.)-&amp;gt;Cloud Recordings tab).  Note that here you&amp;#039;ll also see chat messages.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 5&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
recall:&lt;br /&gt;
 @State variables update their view (and child views) when they are changed&lt;br /&gt;
        automatically&lt;br /&gt;
&lt;br /&gt;
 @Binding variables allow access to the @State variables in a parent view&lt;br /&gt;
 (so, kind of like a pointer/reference)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
GeometryReader gives us the geometry of the enclosing view&lt;br /&gt;
  - what are the dimensions&lt;br /&gt;
&lt;br /&gt;
Note that child views are given a box for where they should draw&lt;br /&gt;
their stuff by their parent view.   But the child views doesn&amp;#039;t have to follow&lt;br /&gt;
these guidelines, it can draw itself wherever it wants&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When we include one view inside another, the parent view can pass data to a child view&lt;br /&gt;
 - By default, data is passed by value, like a function call&lt;br /&gt;
 - we can pass by reference (so changes in the parent are reflected in the child), that&amp;#039;s what we do with @State/@Binding/$&lt;br /&gt;
&lt;br /&gt;
General programming language terminology&lt;br /&gt;
 - call by value: you call a function with parameters,&lt;br /&gt;
    changes to those parameter values won&amp;#039;t be seen by the caller&lt;br /&gt;
 - call by reference: changes to parameter values in the function *will*&lt;br /&gt;
    be seen by the caller, because both are actually accessing the same&lt;br /&gt;
    underlying variable&lt;br /&gt;
&lt;br /&gt;
In C, all functions are call by value, but you can pass in pointers to get call by reference semantics&lt;br /&gt;
&lt;br /&gt;
In python, dictionaries/objects are always call by reference (I believe),&lt;br /&gt;
but otherwise it is call by value&lt;br /&gt;
&lt;br /&gt;
What we&amp;#039;re really talking about here is, how can a function call affect the caller.&lt;br /&gt;
  - do we just care about the return value?&lt;br /&gt;
  - or will there be side effects on the data passed in?&lt;br /&gt;
&lt;br /&gt;
In other words, will the function mutate our data, or is data immutable?&lt;br /&gt;
&lt;br /&gt;
Older programming languages were all based on mutable state&lt;br /&gt;
  - it more closely matches how the underlying hardware works&lt;br /&gt;
  - can be very efficient&lt;br /&gt;
  - but, it can lead to errors/hard to debug code,&lt;br /&gt;
    because you have to consider *who* had access to the data,&lt;br /&gt;
    as any of them could have messed it up&lt;br /&gt;
&lt;br /&gt;
Languages like Swift, and frameworks like SwiftUI, are focused on immutable data&lt;br /&gt;
 - less error prone, a value never changes&lt;br /&gt;
 - but, can be inefficient&lt;br /&gt;
 - magic of Swift and SwiftUI, though, is it is actually very efficient&lt;br /&gt;
   (they achieved this through a lot of low-level complexity that they&lt;br /&gt;
   try to hide but can be exposed)&lt;br /&gt;
 - if you want something mutable, you have to take extra steps&lt;br /&gt;
   and if you don&amp;#039;t do it right the system will fight you&lt;br /&gt;
&lt;br /&gt;
Remember:&lt;br /&gt;
 &amp;quot;let&amp;quot; makes an immutable binding between a name and a value&lt;br /&gt;
 &amp;quot;var&amp;quot; creates a variable that can take on different values at differen times&lt;br /&gt;
&lt;br /&gt;
So let&amp;#039;s inside of a View are fine, because they are immutable&lt;br /&gt;
But assignments to variables are *not* allowed, because they are mutable&lt;br /&gt;
but Views are immutable&lt;br /&gt;
&lt;br /&gt;
Another way of thinking of SwiftUI views is that they are written in a domain specific language (the SwiftUI view language)&lt;br /&gt;
 - so the rules for it are different from the rest of Swift&lt;br /&gt;
&lt;br /&gt;
(To be specific, Views are created using ViewBuilder, which is based on ResultBuilder, a way to create domain-specific languages in Swift)&lt;br /&gt;
&lt;br /&gt;
What is a domain-specific language?&lt;br /&gt;
 - it is just a language designed to solve specific kinds of problems&lt;br /&gt;
   (problems in a given &amp;quot;domain&amp;quot;)&lt;br /&gt;
 - idea is the syntax and semantics of the language are designed to&lt;br /&gt;
   match the problem, and so make it easier to solve the problem&lt;br /&gt;
   using the least amount of code&lt;br /&gt;
&lt;br /&gt;
Say I wanted to create solutions to automatically putting grocery items on shelves&lt;br /&gt;
 - I could just make an API with specific functions to do the operations&lt;br /&gt;
 - or, I could create a little language for describing the problem of putting grocery items on shelves (or anything on shelves)&lt;br /&gt;
&lt;br /&gt;
(Really, custom APIs are DSLs where we can&amp;#039;t really mess with the syntax)&lt;br /&gt;
&lt;br /&gt;
The rules of SwiftUI don&amp;#039;t match those of Swift, even though SwiftUI is implemented in Swift&lt;br /&gt;
 - because Swift lets you do weird things&lt;br /&gt;
&lt;br /&gt;
So, what are we really doing when we make GUI applications?&lt;br /&gt;
&lt;br /&gt;
We have a few key things:&lt;br /&gt;
&lt;br /&gt;
frame buffer:   memory representation of what&amp;#039;s on screen&lt;br /&gt;
                (change values in the frame buffer,&lt;br /&gt;
	        what&amp;#039;s on the screen changes)&lt;br /&gt;
&lt;br /&gt;
input devices:  user does an action, program gets the input&lt;br /&gt;
(mouse,&lt;br /&gt;
keyboard,&lt;br /&gt;
touchscreen)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
our code:       handles input, does output by changing the contents of the&lt;br /&gt;
                frame buffer&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Classic way we organize our code is as an event driven system&lt;br /&gt;
 - user input generates an event&lt;br /&gt;
 - our code processes the event and updates the screen (and internal data)&lt;br /&gt;
 - our code then waits for more user input&lt;br /&gt;
&lt;br /&gt;
If we want to do other things while waiting for input we do it in&lt;br /&gt;
parallel or in small chunks so we can always be ready to handle user&lt;br /&gt;
input&lt;br /&gt;
 - if we aren&amp;#039;t ready to handle user input in a timely fashion&lt;br /&gt;
   we have input lag/unresponsive interfaces and users get annoyed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But how does this map onto SwiftUI?&lt;br /&gt;
 - our code doesn&amp;#039;t directly update the screen&lt;br /&gt;
 - instead, we describe what the screen should look like *at all times*&lt;br /&gt;
   through views&lt;br /&gt;
     - the screen should be a function of some data&lt;br /&gt;
     - data can either be constant or variable&lt;br /&gt;
     - if variable, it should be put into a state variable&lt;br /&gt;
     &lt;br /&gt;
The views are automatically redrawn (i.e., the framebuffer is updated)&lt;br /&gt;
whenever a state variable is changed&lt;br /&gt;
 - it is evaluating *all the views*&lt;br /&gt;
 - this could be very inefficient, except SwiftUI is very clever in&lt;br /&gt;
   how it does updates&lt;br /&gt;
&lt;br /&gt;
When we move to Android development (and later still see iOS Storyboard development), we&amp;#039;ll see a genuine event loop&lt;br /&gt;
 - we&amp;#039;ll have event handlers that will update the screen&lt;br /&gt;
&lt;br /&gt;
And you&amp;#039;ll see the code gets much longer and much more complex&lt;br /&gt;
&lt;br /&gt;
When we say views are immutable, we just mean they are immutable&lt;br /&gt;
while they are being updated - that&amp;#039;s all.&lt;br /&gt;
 - and that&amp;#039;s why we can&amp;#039;t change variables in views,&lt;br /&gt;
   it would make it impossible for SwiftUI to calculate deterministically&lt;br /&gt;
   how the screen should change while evaluating the views&lt;br /&gt;
   (the views can&amp;#039;t change while it is being drawn)&lt;br /&gt;
&lt;br /&gt;
 - we *can* change state inside of closures that are called&lt;br /&gt;
   when events happen&lt;br /&gt;
     - because that is outside of the View updates&lt;br /&gt;
&lt;br /&gt;
So we can&amp;#039;t change things dynamically inside of views, state updates&lt;br /&gt;
must happen as a result of specific events (and thus in the callbacks&lt;br /&gt;
associated with those events)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>