Okay, good. Good morning everyone just barely.
Let's bring up my screen.
I wanted to continue talking about tutorial to
see how this can we get this all fitting in here.
Sort of smaller, smaller.
There we go.
Okay, I'm here.
We have the this is the tutorial for the week so if we, so there's a question of do we have a tutorial for today so look at the schedule but the idea is every week.
I have a tutorial.
Basically, and so tutorial to I started going through it on Wednesday, many of you got through the basic stuff on Tuesday, we're going to continue grading.
You know, getting your feedback, you know after, after we're done with lecture today. But today I want to talk about, you know, the code in there and the concepts and answer your questions and so I know some of you may be, You know, I don't think I think
some of you have questions, So, we're going to work on that.
So let's first.
So, in turn, so we started talking about.
Ya know so yeah so tutorial to is exactly what I'm planning on continuing going through today, and in particular what your.
There's bits of code in here that I have not explained.
And so that's what I want to continue talking about, because as I said before, there's a lot of stuff, You know, in here.
Now going through this code here.
account, upper so we talked about function arguments and the underscore and the return values last time. And this is a basic function.
One thing about for loops.
So the basic format of for loop so you used to be able to do see style for loops in Swift but now that's dedicated. So in most of the time, there's a couple other structures, but most of the thing is is you only have four and then in some set of some
kind.
And the sets can be lots of different things. And notice here, see is a character, that's in a string. And so we don't specify this as a character type but automatically when you do a for loop over a string, you get out the characters in the string.
And
I'll answer your question. So, in this.
So if you do this, then we get a character inside of here and knows we didn't declare see anywhere automatically is declared for the scope of the for loop so you can you see outside of here, it will, it will work.
But inside the loop inside the if we have the upper case contained see so this is doing a contains operation on the string.
On the upper case array, which is much more sorry it's a set.
So it's saying, if the set contains see that we do this up going to the questions here Do you have the two questions for the, the questions for the tutorials.
So every tutorial I'll have these questions. These are questions that you should understand, but I'm not going to ask you specifically about them basically if you understand these questions.
You're doing well if you don't, you should try to figure them out, because these are the things I expect you to understand for doing the assignment. So notice a lot of things tutorials.
We're checking you off to kind of set okay yeah you put the effort in but we're not saying, did you answer all the things because, first of all, it's a lot of work to try to assess all that for you every week, but second of all, it's really about, you
know, you need to take responsibility for learning these things and yeah I'm going to check it on the assignments and midterm and final but, yeah, this is to help keep you safe.
Do I understand things. My you got a question.
Yeah, if you could go back down to the code really quickly. Sure, to the part with the yeah with the lead uppercase equals, or sorry, uppercase, there's definitely, there's a part of the declaration that's, that's a set character.
And I was wondering like if you if you could go over that and if there's like, because we had like, I'm assuming this just means a set of characters but you had like a set of strings, but we didn't have to add in that declaration so could you just go
over that. Right, so if you just so actually, if we had just eliminated this set of characters, this would be valid, this would just be an array.
So by default, if you have square brackets. It's going to interpret this as an array.
What we're doing here by saying it's a set of characters saying it's not an array, it's a set. And so, you know, an array is just, you know, you could think of is a, you know, we would index through the array and it's, you know, and say okay I have a
20 element array and the first elements this the second level it's not, you know, normally starts at zero and we put things in it it's normally a uniform collection of of data, a set is a collection of data, but if you order is not significant.
And it's basically, is it in there or is it not so you can only have one thing in there like you can only put the letter G and then once you can't have two G's in it.
So, for doing this, and internally, this will be more implemented as kind of a, you know, more like a dictionary type thing I'm not sure exactly how swift does it but again we don't, it probably varies, but we don't care but the main thing is, if you're
looking for membership in a set which is logically what this is. This is how you do it now. Honestly, doing this hurt me a little bit, because in a classic language like see the way I'd want to do this is converted to a numerical value, and then just
say, is it in this range. But then, in, in Swift things a Unicode.
When you look under the hood of Unicode, things get weird, because remember Unicode isn't just letters for Latin characters, it's also for, you know, you know, Chinese and Arabic and also includes emojis.
And it turns, and there's different representations is, you know, UTFAUTF 16 UTM 32. It's a mess.
And so it's better just to use the upper level abstractions.
All right, that makes sense. Thank you. So, um so yeah so that's so that's why we implemented this way saying, we have a set of characters, is it in the set.
So swift supports sets arrays, which are more things, and you know basically dictionary type thing so.
So, as here is.
Okay so okay so I'm just going to the questions here.
She left you fill out the word set it would be an array. Yes. So this is a set of characters what you know, so you have said that type of the collection so sets can only have one sets have no order with the different sets of know order, and you can only
have each element present once.
So, but what's more what's different about sets, is if we go to.
Did I have put a link up here.
So we go here, if you go over here.
collection types.
This tells you all of the collection types that they have, they have arrays, which, which are as you expect the new index the array and stuffing iterate iterate over re by using for in.
And then their sets.
You know set can store distinct values the same type of a collection with no defined ordering.
So arrays have a defined morning sets don't. And also, each other can only be in there one time like if you add an element, like you add something to the end of an array like you could add and and and and be, you have 20 copies of them but to a set you
once. And what's cool about sets, is you have safe set operations, you could do like this is a mathematical kind of set you do intersection difference unions subtract sets, you know, so there's operations, you can do on the onset that you can't do on
a race, they really are sets and then there's dictionaries.
So sets I believe should have constant lookup time yes because their underlying implemented with hash tables.
Yeah. Character. The Character part in here is what's in the set. Oh, let me just go back here so I have that another tab.
So.
So when are you saying here when you declare so this is in general, if you have some sort of aggregate type, like a set or an array, or a dictionary, you can declare it and then say what the, what it is of.
So here this is a set of characters, it's an array of integers. Now, most of the time in Swift, we don't actually have to declare the type, because it's because it's all this implicit typing that happens, everything has a very well defined type and you
can't just go willy nilly change it if you try using inconsistently all the compiler will complain very loudly but it does type inference, so it figures out oh this must be that so if you left this out, it would assume this was an array.
It's not an array it's a set so we had to tell it explicitly.
So.
Are we clear on this questions.
So, so notice the kind of general syntax here is when you declare some symbol some variable or something, you generally have it, and then you can assign it be equal to it and if you're going to specify the type, you have colon and then whatever the type
declaration is.
And so here you know sear again like we had s colon string. So you know like that's the top what's the type of s here to string.
Does the type have to be clear, so they're not so if types have to be declared before they are used. We're using built in types here.
So,
yeah, this is just exactly. Exactly, yes it be characters yet yes a montage yes exactly this would be those will be characters but here I specifically saying this is a character and these are the characters I want, I just want it for uppercase So yeah,
those other yeah this could be any character potentially in there.
I'm trying to see all it does s represent the set of all possible strings. Definitely not S is this ass. That's the string that's passed in.
You know whoever calls count upper, which is basically passed in.
Okay, so this is, you know, again sets I assume you've learned from math class, this is just a computer science representation of it.
And then, Um, so we taught this.
So again, notice here, analysis were defined the type of analysis. And notice we're saying it's equal here this is the contents of analysis, and this is a type declaration.
And what we're declaring here is a dictionary, and the dictionary we're doing notice with this.
You know, here we say the key, colon, and then the value so here Colin's getting us in a little different way but this is the key, key type in this is the value type which is a function declaration function that takes a string and returns a string
hello shift.
Command Shift, if you're going to come in and say hi shift actually open the door and the door closed, and she decided to open the door and say hi.
Hello.
Okay, you're getting kicked out.
Kicking off because when she's board she can start biting cables.
Yes, yes exactly that's, that's one of them. Row she's come in and I don't think you've met tab.
Okay, so now let's, let's go back to other parts of this.
Remember this is for the previews don't worry about that.
Now we start talking about this and talking about the views and we talked about the binding variables. So remember I talked about how saw hell. Yep.
I just had a question about the code that you shared with us in line number 55 what is the first string represent 55.
This, This string is this. Remember it's a dictionary.
So dictionaries have keys and values.
Right, right, this dictionary The keys are strings.
And so, and that's what this is saying.
And so, like, and here it is. and the value that you get from the dictionary is functions, and a function that takes a string and returns a string.
And so that's this function here, or account upper second string represents what we provide the function wait and third string represents what it spits out great.
So, repeat what you said there, the second string represents the second string represents what the function takes in.
Hmm. And the third one represents what it spits out exactly the return value.
Then could you go with the first one represents the key of the dictionary.
So, can you elaborate a bit on that. So, do you know what a dictionary is. Yep, dictionary as a key value Right, right.
That's the key of the dictionary.
Right. So like you get analysis you give it the value count, and it returns the function. This function here, you give it the key uppercase it gives you count upper, the upper function.
Okay.
And you can see this because we use it up here. Right here.
This is we're index, we're basically taking a dictionary and giving it the key, which is the analysis mode and it's returning us a function. And then we call it.
Okay, can you use any types of keys then there are restrictions. So let's, so if you go here. So sets.
So dictionaries session keys the same type each case so okay yeah no order for what they say.
The most common thing is strings
or dictionary.
I think you can use, at most, I didn't say the restrictions.
I've to look in the reference man to see what exactly what it is.
I think I'm not sure if you can use a full type as a key forced.
Yeah, just on the same vein, can we look at the definition of the dictionary good.
Other definitely the code. Mm hmm.
The definitely the code. Mm hmm. So it's saying here that we have keys and values the key is the first string and the value is a function that takes in a string and returns a string.
Exactly.
So we wouldn't be able to say, declare here under uppercase just some string and some other string that has to be a function.
Well, when we say a string is the key and then whatever else you evaluate the right half after the colon and it should produce a function.
Okay, so we can't do like.
All right, I got it. Well, it just can't be any function has to be a function that takes a string as input and returns a string.
Yeah. Okay, thank you.
Um, no flute should work fine. I'm sure floats would work strings should work fine.
Yeah, you could yeah exactly if you did string string or double colon thing. Basically you use it.
Think of arrays, as dictionaries indexed by in an integer, non negative integer that's classically what an array is, and that has a very clean implementation at a low level dictionaries basically take that idea and saying instead of us have a non negative
integer, as the index we're going to use arbitrary data and most commonly it's things like strings, but they can, they could be floats or whatever, it means they asked me anything that you could feed into a hash table.
And what the hashtag spits out.
Thanks to the hash double protocol. So, One thing to
a lot of things in Swift, if you look at the, like, what what how things work, you'll find yourself eventually learning about Objective C. And that's because Swift is not designed to be a standalone thing it's actually built on this giant foundation of
stuff, which is what has been built on iOS and Mac OS, all in Objective C so a big thing that swift does is connects to Objective C, and a lot of types and swift are actually thin wrappers around things in Objective C.
Objective C is beyond the scope of this course it's a very interesting language.
It's inspiring. It's basically a combination of see and small talk.
But yeah, and it's very dynamic it's got lots of cool stuff in it but, yeah, the underscore I'll do the same but forest. Yep.
Sorry, I forgot to put the hand. OK, So the, so the underscore in line 40.
So the, the underscore in line 40, this is, this is talking about. If you go to function declarations.
By default, in Swift.
When you declare a function you have, you know, basically, you know the variable and colon and then the type. But when you call functions like this, you have to give it a name parameters.
By default, so if I define a function greet with the argument person. Even I call greet.
I have to say person colon and the value and the underscore is how we basically go like, I just want to position on one I'm not going to specify the name because that's just annoying.
So that then they were like, way down here.
Function types.
Know what part of this.
Oh, here we go.
Yeah, so I keep like this underscore here.
It's really basically to save time, it's really to save time because it's basically looks at make it look like morals are fun so here, like if I want to add two integers.
So for example, I want to add a and b, if I don't want to say a colon number and beat colon that I use the underscore to say is gonna be the first one, then these will be the next one.
Yeah, it's really to save time, or wait but it's all time, it's just, it's too. It's not so much time it's not about reducing the number of keystrokes although certainly, it can do that.
It's to improve clarity sometimes if you just have one argument set to a function, having to remember the name of the value is just like annoying. Like, why do I need that just, here it is.
Take it.
So it's a style thing, and style. Remember programming languages as a thing are so this, we, there's often things that programming languages, our way of talking to computers.
That's true.
But that's only half the story programming languages are as much about ways for people to communicate with each other about computational ideas. And so, clarity of code matters a lot and that's why there's a lot of variation in syntactic sugar and other
things is, it's to make it clear.
And so do what's clear is this is actually what I would say whenever you're, you're coding stuff and if something isn't clear it's like maybe using different.
Now, I kind of find this underscore, a little annoying, but I do appreciate the emphasis on named parameters because I think people suck like if you're having a function with more than two parameters, I mean, if you don't use name parameters, is just
pain.
Oh, okay. So can we omit exclamation point when we call analysis and content view.
Ah.
So can we eliminate that. That is worth bringing up x code and showing you what happens.
Okay, let's
text analyzer coming up.
Let's go to not not content view.
So, here you are with the code.
So we're asking is, there's an exclamation point here. What happens if I get rid of it
should give me a nice little error shortly.
Yes, there's my air value of optional type string string must be unwrapped to to a value of string string coalescing using your pride fault as an option balcony nil for morass using this to abort execution of the option of it contains now.
The error messages in expert well.
They're terse.
So, that's so that's what it means the unwrapped so this is what we were talking about earlier, remember about optional types, so.
So, yeah, so what it what it really means is,
OK.
One second.
Feel free to unmute but someone was talking.
I'm remembering swift references normally refer up.
This is the thing, always remember about this. So we call this thing being unwrapped this wrapping idea is, is this idea of that. It could be the value, or it could be know in other languages like Java and Java, you have a reference the reference could
could be the thing, the object you want to refer to or it could be no, and see you have a pointer, you could be referring to the thing you're pointing, or it could be a no very you know a no pointer.
If you leave out the check.
You have a runtime error.
not really the idea of this is we want to make it so that if it's possible that somebody sent me know.
If we leave out a check for it we don't want to find out some point when our programs executing we want to be able to say, it could be here. So you better do something about it.
So, the, and this is, we talked about this with the first tutorial. This unwrapping of types go back if you didn't follow that there So, but I'm showing you a different way of handling this, and the different way of handling it is I can use an exclamation
point, which is basically saying, I know this. No that's wrong there.
And then
there, that's your face, so.
So yeah, I agree, the error messages can be a little puzzling, but
there.
I find its swift it the error message are hard because it's built on this funny conceptual landscape, the conceptual landscape is that have strong typing, where they want strong typing, where they want to turn as many errors as possible into compile time
errors, not runtime errors and so in effect, you're getting this bit of static analysis on your code, would you don't get in languages like Java you definitely don't get languages like Python.
It's really sorry that's really only newer languages, but you will see it this term, both in Swift, and in Caitlyn.
So t know. So how does team, know, know that t is an optional. So the reason it knows it's not that t is optional. That's not what's optional here at all.
What's optional. Here is the return value of the dictionary lookup.
That's what it knows, and it knows this because when dictionaries can when you do a look up in a dictionary, you can get the valid value, or you get nothing because what so in this new it just understanding the data structure.
What if I look up count, I'll get a value if I look up upper case, I'll get if I were if I look up, you know, shift.
Look up shifting here.
It will be there because we didn't define the value, so you can always look up in a dictionary a value that isn't in it. But the return value will be nil.
And so you better be prepared for it and the way that's encoded in Swift is that whatever the type of the return value of, of this. So we said this returns a string, it doesn't actually return a string.
That's right, that's right.
Actually that back, it doesn't actually return a function, it returns a function, wrapped in optional type basically it could be the function, or it could be nil. You don't know which.
So, and then you must decide whether it's real value and then go on so.
So it just means that the value isn't founded abort the functional program, no it does not make it. The default, it says that the value is not there.
Oh, sorry. Yeah so excellent makes it what makes it so that the value is not found it just supports the, the program Yeah, basically, if you use an exclamation point.
And it turns out to be nil, your program crashes.
So, it has an effect by using exclamation point we turn it into something like our Java would behave.
So it'll just so it'll just crash so whenever you see the exclamation point you better be darn sure that you know what you're doing.
But I can do it here because I've constrained things that I know that T can only taking on certain values, sorry not to analysis mode can only take on certain values here I've assigned it to count.
And here, you know, and then the analysis menu is built on the keys of here. So, I know that.
know that I can actually try think how I could make it so that.
Watch this.
Let's go.
Let's do this I'll change this here now counting is not in our
think so.
So I built this I put this there.
It should come up with a second.
Oh, goes.
So it's gonna launch the program.
And what I expect the program to do is crash immediately.
And it should ideally crash into the debugger here, but it might take a second.
So let that run for a second. And, but why will it crash.
They don't see why it will crash.
What's different in this build is I change count to counting their.
Yeah account is not defined the dictionary.
And so here this is what just happened here and look at here, it so this is so the app crashed and it gets connected to, to, here and now we can actually see the error here.
So, fatal error, honest roughly found nil while unwrapping an optional value.
Fatal Error unexpectedly found near a wall and Robin optional value.
So, this became a exactly this is a runtime error we're in the debugger.
Now I can actually go through the debugger in. If I want to debug this and see exactly where it is, you know, but honestly going through this is kind of painful.
Wow, that's a lot of frames.
So here we go, switch our runtime to port closure and closure there. Yeah, I would not meet you can debug but this yeah you don't want to do this. Wow, That's really deep 65 frames.
So,
okay, so.
Yeah. Yeah. But basically, so.
So, I will tell you at so let's actually do this as a. Let's do this right here
to say
let.
copy this,
that.
But then we'll do a.
Oh.
says.
Here we go.
We should we say that this should be fine I think I fixed the air.
Yeah.
So notice what I did here is I've now said analysis function is equal to analysis and mouse mode or count upper. So, if I make this now.
I find now make this counting.
And let's see that.
Now I've said as I've said the default function to use is count upper.
Yeah, it was only allowed to compile in the first place because the exclamation point exactly. So notice now I say.
So notice that it says counting.
But counting is now it's acting like it's counting the number of uppercase.
But now if I go here let's fix it let's go to count.
Now it says 22. And now we go to uppercase, that does the right thing.
So, this is, I would argue, whenever you're doing, and
you can do exclamation points, but it's generally considered always call us code smell to have that in there because you're basically saying, trust me it's not going to be new.
And you're hoping the rest of your codes correct because if it ever does.
You're in trouble. So this is what I call defensive programming, but here this assumes that this default value is ok. If this isn't Okay, then.
Then you need to really no
need to figure out what you're going to do the question is, is that value is there what makes sense for your program you could do it if lat or something and try crashing the program or something but it was just fix this
question mark question mark is not handling exception. That's not what it is what it is is question, question mark question mark is defining a default value.
Normally in Swift you aren't dealing with exceptions where there's some error handling code separate from the main code that generally air you handle the errors where they are, is a question one question mark is attorney area, so it's basically one question
mark in the you have to have a colon Yeah, that's a that's a default it so I'm
green What do you talk the counting just disappears.
I think I think you might have missed something there
in the app. Okay, so this is the old bill. So we'll bill let's exit out here.
Let's put back to counting I'm trying to remember how to do a task which are there in the simulator.
But let's say we go back here.
Build it shows up in the app here so it's.
So now it says counting.
Yeah, this is the default mode.
Yeah, exactly. So here it disappears. And then we can only get back those values, because they're the ones defined by the menu because when we selected the menu it selects those things.
So let's move on to other parts of the code.
Okay, good.
Now let's talk about these views. so there's a question about states.
Any view.
So, when you have two views. You can connect the views together and note that we have a state variable here analysis mode.
Analysis mode is basically part of the larger containing view.
And then we've created a there's a sub view, which is defining the actual menu, and we want the menu when it's updated to change the analysis mode, depending upon what selected, but since this isn't a separate view by default, it wouldn't be able to access
this variable directly, and if we declared a state variable and here it would never be able to be used to.
It would be will be used by some other stuff in the main view because it'd be private to it. So the way we connect these things is by, we have the state variable here, we have a, we have a binding here, and then we're connected because mode menu is included
in the, you know, in the content view view.
And so that's what the mode menu so we're, it looks like we're calling it, but we're not really call it gets a struct. But what we're doing is we're including modem menu in the view.
And as the arguments we basically say, how to connect any variables between them. And we're saying the analysis mode binding here, that's what this is referring to is the binding variable.
And that gets bound to analysis mode here, but you use this dollar sign to refer to it, and you go like this doesn't sound like regular function calls and it's not, this is weird swift UI stuff, which is defined in a sense of its own little language,
so don't expect this to follow the regular swift rules because it's it's technically following swift rules, because all implement in Swift but it's using some fancy mechanisms that you don't use.
Otherwise, in your own code.
So binding is a is basically like passing a reference Yes, you can really think of a binding as a pointer. And when you do it here, we're now basically saying okay we're giving up the dollar is like giving the address of this.
It's not that. Technically, but it is that conceptually, what is the padding do up just play with it oh by the way.
You don't have to do all the things in do all the stuff there's this whole.
What's Hi this new
high navigator, and view.
Show, Inspector.
There's a way to turn off this. I actually really hate this you how to get rid of this but anyway. So when you look at the here if I click on this here.
It shows this here. And so,
why is it not
work.
Really.
Oh, so this knows how this is pushing where the text is like this will actually show you what the that there we go.
So you'll see now it shows. This is inherited you can see this is you know
how annoying help
this identity and type.
So you oh you can do things like change the alignment here.
It will change like you can do things here and notice how it will
change the code as you mess with it. So here's the padding, I can get rid of the padding. And now it's not padding, but now I can say.
Now there's patterns that doesn't padding just basically increases the bounding box around any object so you can see exactly what the pattern is done doing your padding all you can say I don't want padding on the, you know, on the top on the bottom.
So let me just disable that. And so now it does that and so now you'll see it updated to now shorter here. We do this year.
So, if you want to play with the way things look.
You can definitely mess with it here and change things here and I think we can even do something where we take this font. Big and say wait color.
So we can make this a large title
should update here, and it goes bigger, and notice it will say font to large tile so like, it's actually directly changing the code as you mess with it.
So,
this is this is where this is really useful.
Oh hi the mini map with the control shift command.
Oh my god.
Control Shift command command.
Thank you.
Yep, that worked.
Is there any way to import fonts or images have to be used,
you definitely can import fonts, but remember you got to import them programmatically.
So your app has to add the font. I, you definitely can add fonts, I have not looked up how you do font handling and swift UI. I am sure there's a way of loading arbitrary resources as fonts.
So, so, um, I would do a Google search and look at how swifty Why does the housework UI does that but yes no you definitely can do different fonts here, this is not HTML.
You have full control over things up.
This is a general thing about mobile about well, when you design graphical user interfaces in general. How do you do it because you're oftentimes specifying in some sort of tax declaration.
And then you want to have.
You want to be able to see the results. So classically this sort of thing would be possible you'd have this you know thing that shows you what your stuff looks like.
But the language here would be something like act like HTML like. So, in Android and iOS for a long time this was essentially XML.
And, but it's some sort of declaration thing which specifies it and then your code would be a completely separate thing. What now the move is towards making it all a unified view, and that's that's why when you mess with things here in the preview here,
you can. When you make changes here, you can see the functionality directly changed in your code.
And it's nice then it's because it's in your code, you can understand the declaration. You can use it later and you can easily change it, and it's right there beside the functionality as we see when we go to Android.
It's not so clean and again if you do it in iOS storyboard mode, which is for UI kit which is the older way of doing things, is not like this.
Yeah, yeah, even HTML you can import file that's very very true.
Okay so, so this is one view this view is included in the other view here. Now, another weird thing in here is this for each.
This for each unit. It's a loop. Right.
But not.
And the reason it's not, is because what it actually returns is a view.
Well, so what so what a menu is actually a menu is a list of views that are.
So it, so a menu is of it. First of all, the menu is this thing here and then when you click on it, it has a bunch of us inside of it. and those pop up.
So when we click this and I see what if I click it does it does it work I don't think I'll work here but if I go to the simulator, and I click on the menu, notice what happens it pops up, and it has these two things.
What are those, those are buttons.
So what we've done here is we've said we want to have, you know, we're going to have a menu of buttons, but what I really want to do is, I didn't want to make one button Why could is instead of doing this.
Instead of doing this what I could have done is done.
So.
Oh, I can't actually do okay I can, I can do that.
Know.
done this,
hide this inspector here, because we're not using anymore.
Oh, I'm being stupid.
So.
Oh, sorry.
There we go.
That's right.
Yeah, no, I'm not either.
So this should work as before.
extraneous.
My syntax errors.
We go here.
And now, This works as we expect.
So works fine.
So why is the auction mentioned in.
So, so the action is supposed to be.
This is a function.
Here I'm defining it as a. I'm actually defining it as a closure.
And it's a function that takes no arguments, so we don't have to have in and specify the ark the things going in. It's just a function that's going to be called.
And so, yeah. Correct. Thank you for the syntax things I was looking at that but you'll notice that this action here.
It takes a function as an argument. And so we declared a little function, but the function is really dumb, it just assigns a value to analysis mode but it knows it assigns the value to notice the, the scoping here we can access analysis mode here, it's
referring to this one that's local within this context, and this one. It's here, it's referring then up to here.
Private. Note that you can't declare this private.
Don't think.
Yeah.
Yeah. If you declare this private that means that can only be accessed inside this view mode menu initial you know initialize it accessible. So, make sure this is not private access its access outside of the view.
Okay, could we have, you know, kind of done uppercase equal to count up or no, because remember we got to put it as a string here, we use that string to look it up here.
But analysis mode is a string. And that's the data shared between them. It's not a function I was being stupid and forcing. Isn't it a function No no, it's a string.
Which one is better for space, This way or the first one. I'm, I suspect they're about the same space wise because the compiler will optimize them away.
I think probably runtime wise, they're probably exactly the same because again this can all be figured out compile time, but from us.
This is much stupider.
This is the stupid way of defining these buttons because we had a male ego the menu if you add something to the menu. Now I have to manually add something in so this is dumb.
So whenever you have something like this and you want to generate a bunch of views it here, buttons, in this case, programmatically. That's where you use a for each, but it's not a regular loop, it's a swift UI construct for generating things, but it's
got a couple of weird things.
This, this year.
Now what happens if I did it this way.
This is still work.
I only have a guest wow this code will run.
Yeah, I think it should have duplicates.
See for right.
Whoa.
Six.
I did that happened.
It's not quite what I was expecting. Why is that.
Oh, because these buttons are inside the for each.
Yeah.
So we're getting each time we're getting a button, and we're getting for each iteration loop. So actually these should be, you know, this is
if we did this.
Now, this should be what we were expecting.
And,
yeah, yeah, uppercase count, count uppercase and all the buttons should work.
So, the four. So notice what the how the syntax of the for each.
Again, we have a closure. So this is why you need to learn closures and be comfortable with them because you just show up all the time.
There's there implicitly all over the place.
So this is a closure here, we'll talk about this ID here in a second here.
First of all, there's the available modes. So the available modes, is basically the keys, the keys of the dictionary, as an array.
So, so what we have is analysis of keys. And then we're basically coercing it, if this syntax is a little weird to me.
Oh no, this is you're declaring this array of strings here, and then you're basically saying this is what this returns. So, that's a little funny, but that's how you convert this into an array of strings.
And then, um,
yeah I had some problems originally in Swift where I thought it's like why is that an array of strings already but no, you have to specify the type coming out of this.
Because otherwise it's something weird up.
But that so this is an array of strings we order for each over an array.
And then we have a function here where the mode and we generate the button. So this is all the same. They're the weird part is this ID self, and it's because when you do a for each it expects to get an array of objects.
And that array of objects, you have to
have to know on what property of those objects basically a set of structure or classes like that it's iterating over and so it expects to get a property of it.
And so this backslash dot self basically means it's the thing itself that you should use to iterate over. So that's how you, that's how you distinguish the elements of this.
Do dictionaries and swift maintain.
Maintain insertion order.
You should not assume they do, they're not supposed to preserve order so if it does it's an implementation thing that may or may not continue to work.
Okay.
Yeah, so the for each is where we should do we should do this is yeah don't do these
the stupid way it works. But it's stupid.
Okay.
Yes, exactly. Whenever analysis mode updates the content you will automatically update Yes, exactly. And this way. Also, if we were just to dynamically add elements to analysis right here we've declared it as static, but we could declare this as dynamic
and we could men, we can add runtime, add elements to the menu and know what elements to this dictionary. And if we did the analysis menu would automatically update we wouldn't have to do anything at all, because it's be generated by the for each.
So, normally in, you know, as a programmer you use loops.
Instead of things but I'm saying but this loop don't treat this for each. There are for each loops in Swift for iterating over things.
But there this notice the capital for each, that's a different thing.
You guys after for each loops, the left here the view the body binding menu.
Notice the menu is very dynamic you could put almost anything in there we just happened to put buttons might be fun to try putting other things in there and see what happens.
Because it turns out, essentially any view.
Well, if you did something else you'd have to specify you figure out how to get it to do something, would you put in spaces or other things like just the arbitrary stuff the fact that the button works when it clicked is a property of the button, not the
menu.
You put images in Yep, definitely put images in
trying to think of other things to show you with account now that's actually most of what this code is and it's good because we're. We've been going for over an hour.
So, my plan now is I'm going to end lecture now and yeah you can put menus, you should be able to.
I have.
I haven't tried making message menus like that but you should be able to define a nested menu like this.
The last thing to remember is, notice how we again aren't controlling the execution of this. We're just declaring how the interface works, how the pieces are, and we're defining callbacks essentially functions to happen, that that run when certain events
happen such as when a button is pressed.
So that's where the functions that have run here, right here when a button is pressed. That's what we do here, here when we want to update the text, basically whenever this is we call a function to get the value of what the text should be, but otherwise
we're just sort of declaring this is what things look like.
Okay.
We haven't made nested menus. So the idea is that these menus are just it's just a collection of us there's no reason you couldn't have put.
So the question is how to invest in menus, you could have made this mode menu you could insert it here so this being a button This could have just been another menu.
Nice orthogonal that way you can make things where you think you can put something there you can't and that's part of the joy of having everything be views.
Why can it update dynamically, which we can we can which update dynamically intro the menu what the contents of the menu will update dynamically. Remember, essentially, every time the screen has to change.
Again, so.
So, the analysis so if so the analysis structure, like if this analysis dictionary, we were to add something to it, it would do not dynamically update, because remember the entire screen is updated, or whenever it needs to so this.
In order for the system to create the screen, it runs our code.
And it runs our code, potentially, all the time we don't control how often this runs.
It will optimize it so it doesn't run it all the time you know when it doesn't need to just win the state changes but like if you were to switch apps or I think it will run this whenever, and by the designers with you I don't care because it's static.
So, the by, so in terms of why are these connected this is swift UI this binding in state thing. And that's you know reference between to view the two.
These two strikes.
And if you look under the hood, the stuff behind this state and binding stuff is pretty complicated.
Right now, we'll just think of as they're just, these are decorators, and again, part of this is making a Stockley type language do things dynamically,
which is a neat trick.