Swift Programming Basics: Structures (Lesson 8)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome so far you've learned about variables and constants and how they're used to keep track of data you've also learned about functions for organizing and grouping together your code statements well in this lesson you're going to learn about structures or structs for short and these are the basic building blocks to represent your data or to represent something in your app structures bring together the functions the variables and constants everything you've learned so far in the last two lessons all into a neat little package now just before we begin in case you're new here my name is chris and welcome to code with chris the place to be if you want to learn how to make an app quick question for you how are you enjoying swift coding so far are you understanding everything let me know by leaving a quick comment below and on your way down there please give this video a thumbs up because it helps out more than you know so thank you all right with that said let's dive in and see what structures are all about now let's do a quick recap first you had code statements like these and then i showed you how to use functions to organize and group them together now with structures you can group together your functions you can also have variables and constants keep track of data inside your structure but outside of any function and we're going to talk about these a little later in this lesson now let's take a look at our view update life cycle diagram again for the view code that represents your ui you might create a structure to represent your home screen and all of the view code for your home screen would go into that structure if you had a second screen in your app you might create another structure for that second screen and all of the view code for that second screen would go into that structure in this case each structure represents a different screen in your app now let's move over to the data retrieval part of this diagram you might create a structure and call it a data manager and you're going to put all of the code and functions related to retrieving and processing the data inside of that data manager structure in this case the structure doesn't represent a screen in your app instead it represents a crucial component of your app so as you can see structures are very flexible and lightweight and they're used all over your app now let's go into an xcode playground and take a look at how we can declare our own structure all right so here i've got a brand new empty playground let's take a look at how we can define our own structures first you start with the struct keyword followed by a space and then the name of your structure so i'm going to call this one my struct and then you put a space and you open up a set of curly brackets inside the curly brackets you would put all of the code in your structure and that's it we've declared our own structure now before we move on and take a look at what's inside the structure i want to point out the naming convention of the structure notice that i've started it off with a capital letter this is the standard convention this is different from the camel casing that we used for the variables and constants and the function names those started with a lowercase letter and it had each subsequent word starting with a capital letter whereas for structures it starts off with a capital letter and then each subsequent word has a capital letter all right now that you know about naming structures let's go inside and take a look at how we organize the things inside of the structure so usually at the top of the structure inside the curly brackets we would declare here all of our variables and constants used to track data that is related to this structure now these have a special name as i mentioned earlier and we will get to that later on in this lesson so up here after you declare all of the variables and constants tracking data for the structure the next section you have are all of the functions related to the structure now there are no clearly defined sections in a structure you know all of the code really just goes in between the curly brackets but usually this is how you would organize all of the different pieces of code inside of your structure so at the top variables and constants and then at the bottom all of the functions so as you can see structures are great for grouping together functions variables and constants are related for one thing but structures are usually meant to represent something in your app in the view update lifecycle diagram that you saw earlier we saw examples of a structure being used to represent a screen of your app as well as to represent a crucial component of your app like that network manager so why don't we do another example and i'll show you a structure that is a little bit more concrete let's model it after something so earlier in the view update life cycle diagram you saw that we could use structures to represent a view in our app so let's go with that let's have a hypothetical chat app and let's say that this structure represents my chat view so i'm going to change the name of my structure to chat view now under variables and constants i might have a variable to keep track of the message like the chat message that i'm typing into the chat box so i'm going to call this message and the type of data this would be would probably be a string and i'm going to assign it an empty string nothing in between just two quotes so that's what's known as an empty string now under functions i could have groups of code that would perform different tasks on this screen for example maybe when the user taps on the send chat button it would execute some code to send the chat message to the server so i would declare a function so that's funk followed by let's call this send chat and two rounded parentheses and i'm going to open up a pair of curly brackets and inside here i would put the code to send the chat message and then in between the variables and constants and the functions i would have the view code for this screen that's all of the code for the ui so now in this chat view structure we have a neat little package containing all of the code for that one screen now earlier i mentioned that the variables and constants that you declare up at the top of the structure they have a different name so now i want to tell you what that is this variable declaration that i have up here message this is known as a property of the chat view structure if i had additional variable or constant declarations those would be known as properties of the chat view structure as well and down here functions actually also have a different name a function inside of a structure is known as a method of that structure so this send chat function is actually a method of the chat view so now let's just update our comments here so that we use the proper terminology instead of variables and constants up here i'm going to rename this well not rename but just retype my comment and call those properties and instead of functions these are going to be called methods just so we're clear now inside of a structure properties and methods can actually work together to fulfill the duties of the chat view let's take a look at some of the special ways in which they can work together now let's take a look at this send chat method for instance if we were to write the code here to send the chat message it sure would be handy if we could access the actual message in this message property right well we actually can so if i wrote something like this print and then inside the parentheses i put the name of the property in fact i can access that data and the reason for this is because this property is declared in the scope of this structure so the scope of this structure is anything in between these two curly brackets essentially the opening and closing curly brackets of the structure so any of the methods that i declare in here for instance if i declare another one let's call this one delete chat because these two methods are also in the same scope you know it's inside of the scope of the chat view i am able to access the property the properties that you declare up here are accessible to everything within the same scope so that includes all the methods that are declared down here now i have to say that each method has its own local scope so this send chat method has a scope inside here and this delete chat method has its own scope in between these curly brackets so if i declare a variable inside my send chat method let's say var prefix equals chris says and then let's say i use this prefix and i prepend it to my chat message so the entire chat message would be chris says and then something so maybe i'll print prefix plus message to get that sort of effect and i wanted to do the same thing inside delete chat if i try to access the prefix variable and type print prefix plus message down here inside the delete chat method you'll see that it xcode complains and it says it cannot find prefix in the scope because this variable is not declared in the same scope it's declared inside the scope of send chat so how would we fix this well one of the ways we could do that is to turn this prefix variable into a property that we declare at the top of our structure you know move it outside of the scope of sendchat and put it up here into the scope of the chat view instead so now that i'm declaring my prefix as a property of the chat view you can see that the errors go away and i can access this prefix property inside both send chat and delete chat methods now i want to talk about another type of property first let's define what these properties up here are these are called stored properties and the reason is because when you access these properties and you reference them by their property name it just returns to you the value it's stored there's another type of property where when you access it it needs to compute or calculate the value before it returns it to you so let's take a look at what this second new type of property looks like so let me start by erasing this prefix property up here and deleting that and then i'm going to declare this new type of property it starts off just like a normal one you use var space and then the name of the computed property i'm going to call it message with prefix followed by the name instead of assigning it some data you open up a set of curly brackets and here you can put the computational code to compute the value that you will return when this property is called one thing though because the value is not immediately known xcode can infer what the data type is so you actually have to specify the data type after the computed property name so after message with prefix i'm going to put colon and i'm going to put string because that is the type of value that this property is going to return this is different from this stored property up here message where i can actually use the shorthand and erase the data type so it's just var message equals string and i can do this because i am immediately assigning a value to that property so xcode can look at that value and it can determine and infer what the data type for that property should be with a computed property i have to explicitly specify the data type all right so for my computed property message with prefix let's take a look at the code inside of the curly brackets here i am going to use the return keyword just like with functions right i'm going to return chris says so this is a string plus message so now in my send chat method instead of printing prefix plus message i can just return message with prefix same thing for delete chat instead of prefix plus message i'm going to return message with prefix so every time this property is accessed it is going to run the code inside these curly brackets and return that as the value for that property it needs to compute it that's why it's called a computed property now with computed properties there's also a shortcut if there's only one line of code in here then i don't need the return keyword because xcode can assume that this single line of code will output the data that i want to return for my computer property so i can actually just delete the return keyword however if i have multiple lines of code then i would definitely need that return keyword because xcode doesn't know which line of code is meant to be the the value that gets returned so for example inside my computed property if i say let prefix equals chris says and then down here i have prefix plus message it's not going to know which code statement returns the value so i actually have to use the return keyword like that and that's going to be fine now as far as computed properties go there's definitely more we can talk about but this will suffice for now in later chapters in this course we'll definitely go over computed properties again all right let's do a quick recap before we wrap up this lesson you learned how to declare a basic structure you learned about properties and methods in a structure you learned about scope and you learned about computed properties as well now i know that the hard part is wrapping your head around these concepts as we code further together you're going to see these concepts put into practice to help you remember what you learned today i highly recommend that you take the quiz and the challenge for this lesson and don't forget you have the swift cheat sheet as well to get all these extra resources just visit codewithchris.com and sign in or create your account and enroll in the 14 day beginner challenge finally if you like this video please give it a thumbs up and don't forget to subscribe and turn on bell notifications i just want to say one thing before we end you did it if you're brand new to coding these three lessons were the mind-bending ones in the next lesson i'm going to show you how these concepts relate to your actual xcode project alright i'll see you there
Info
Channel: CodeWithChris
Views: 17,660
Rating: 4.9780221 out of 5
Keywords: swiftui, swiftui tutorial, how to make an app, how to make an app for beginners, how to make an app from scratch, how to make apps, how to create an app, how to build a mobile app, how to make an iPhone app, Xcode, iOS development, how to develop an app, app development, mobile app development, swift, build your first app, codewithchris, code with chris
Id: voviIrX7bXU
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Thu Dec 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.