YTread Logo
YTread Logo

Swift Programming Tutorial for Beginners (Full Tutorial)

May 01, 2020
They begin with a capital letter, so we must follow the same convention. I always start class names with a capital letter and that's different from what we've been doing with variables, constants and functions. Okay, so I have these two keys like this, what do they do? I put it inside my blog post class, well why don't we put these things here? I'm just going to cut them and paste them inside here and then I'm going to delete them here and I'm going to delete this text inside these variables inside my class and I'm going to leave them empty and I'm going to explain why in a second, okay, so we have defined a class called blog post and this class has three properties, now a property is just a variable declaration like this, but inside a class it is called a property and you will see why in a second it makes more sense and because the title of the blog, the blog body, the blog author is inside the blog post, it's a bit redundant to name them. like this, so I'm just going to name them title body and author like this, okay, this represents our blog here now that you've defined what a blog post is, let's create an actual blog post because remember this class definition here. this is just a definition of a data type, you are defining what blog post data type is correct, so it is not a real blog post, just like this is a string which is not the chain definition that is a real string, so to create a real blog post, we'll write the class name followed by two square brackets like that and so, this is a new instance of the blog post type and it's called an object. or a blog post object, so when you define a new class using the class keyword, which is what is known as a class definition or simply class, but when you create actual instances of that class, they are called objects, you can You think of your class definition as a template or a blueprint and you use that template to create actual tangible blog post objects, so let's get back to the playground here.
swift programming tutorial for beginners full tutorial
This is a very important concept to understand. We have defined a class called blog post. Here you have these three properties or you can think of them. as attributes of a blog post and then down here, like this, we've created a new blog post object. Now this blog post object has a title, it has a body, and it has an author, but they are all empty right now, so what? What we're going to do is assign this blog post object to a constant, let's call it my post like that, so what we've done here is create new blog post objects that we assign it to. my post and now why don't we try to set this blog post title and body and author?
swift programming tutorial for beginners full tutorial

More Interesting Facts About,

swift programming tutorial for beginners full tutorial...

What we would do is say my post correctly, which refers to this new object and then we press dot and that allows us to access those properties. which we have defined in the class Stephan set the title - I don't know hello playground I think that's what we had before let's set the author of this and set the body - hello okay just hello now if I print my post dot author would get my name now let me show you something else, we can create a second blog post instance or a second blog post object and let's call this my second post and we're going to set this up as a new blog post object like this and we're going to Say the title of my second post is "goodbye playground" and we can set the author as someone else, let's say John Travolta.
swift programming tutorial for beginners full tutorial
I don't know why that suddenly popped into my head and let's just say hello again, now this is a second one, my post object, this is different from my post, these are two completely different blog post objects, they both contain these three properties that we can set because that's what we've defined here in this class. Now, another really cool thing about classes is that you can put functions on them, you can see how all the lessons so far come together. Let's first create a new property for this. Let's say a number of comments is equal to zero.
swift programming tutorial for beginners full tutorial
This is the number of comments on the blog post. We're going to define a new function in this blog post class, we'll say add comment, okay and we're not going to worry about the comment text yet, let's just define this, so notice that in my function definition I have these two keys. Again inside here, this is going to be my function code, so inside this function I'm just going to increase the number of comments by one, all of this is everything that you should have already learned in the previous lessons. Okay, now each blog post object is we're going to have this function now, how do we access it?
Let's just get to my posting point. You can see now in the autocomplete that there is this function called add comment and it doesn't have any return value, it doesn't return anything, so the return type is null right there, let's call this function, okay, see what happens, so add a comment, you can't visibly see what's happening, but let's print the number of points from my post, you can see it's one. Okay, now let's print the number of points from my second post. comments What would you expect this to print? Print zero. Why, because we haven't called the Add Comment function in my second post, we just made it so that with the first post each of these blog post objects keep their own properties changing the properties of one object doesn't affect the other one.
Even though they are sliced ​​the same, both types of blog posts are two independent entities and maintain their properties separately. Now there are many more classes, but fundamentally I want you to understand what. They are, so I'm not going to make this video longer than necessary. I just wanted to introduce you to the classes. I hope you can see why classes are a critical component of organizing your information. Thanks for watching, please provide this video. Thumbs up if it helped you and subscribe for more. Hello, welcome to learn Swift lesson 10 for


. In this video, you will learn about inheritance, also known as subclasses, which allows us to build on previous classes that we have already built. defined and saves us a lot of time, effort and work so that we don't have to keep defining classes that do similar things.
Okay, so let's get started and see how it works. Let me start by giving a little summary of what you're doing. I have learned in this series so far, so learn about variables and constants to store and keep track of data. You also learned about some control flows and conditional statements so you can express logic and make decisions with your code and then you learned about functions where you can define blocks of code that perform specific tasks as a way to organize your code into more manageable chunks and then you learn about classes that are further where you can organize related code and related functions together as well as related variables in the form of properties and then today you will learn about inheritance, also known as subclasses, as a way to further organize your code.
Okay, let's take a look at inheritance in a playground, so I'll start Xcode here. I started with a new playground and let's call this legacy playground. Okay, let's save it there on my desktop and delete this line of code, so let's say the application we're creating is like a car simulator or something like that. and we need to define a class that represents a car, so we'll start by creating a car class like that and some of the things it can have is, for example, it needs to keep track of the top speed because it's different.
Cars can have different ranges for top speed. I'm just going to initialize that to 200, so there may be a bunch of other attributes or properties related to the cars, but for demonstration purposes I only need one, so I'm not going to go any further than defining the maximum speed there and I want define a function so that the car, by definition, is a vehicle that can be driven correctly, so I am going to create a function called drive like this, it does not accept any parameters and inside this This function will execute all the code that we need to drive a car, but for this demo I'm just going to print the driving and then put the top speed like this, so this value is at the top.
The speed property is substituted there and then it prints the driving at whatever value is 200, so what I call the driving function will print this statement to the console. Okay, so we have defined our class here that represents a car and in the previous lessons you learned how to create a new object directly from the car class, so why don't we declare a constant called my ride and let's create a new car object and we assign it to the constant my? travel like this, we have created a new car object, now let me try to access the maximum speed.
You hit the dot and then you'll be able to access the property there so you can see that it's running the playground. Okay you can see 200 there and let's try to call the works with my trip point and handle that and you can see it says drive at 100 okay so now let's say in this driving simulator app I also have something called super car or maybe a car from the future, so it's the car of the future for example, and it can fly, so it's the flying car that will probably come in the future, so we can define another class to represent that and we can call it car from the future , and again this car of the future because it is a car, it will still have a top speed and let's say this top speed is 250 like this and it also has a function called drive and since you can still drive this car and again it will just print out, you know, driving at maximum speed and also this car can fly, so let me define another function that represents, you know, flying the car, so fly, I'll just call it fly like this, so here we have defined our car class, we have to find a future car right here, if I want to create another one, let's call it my new vehicle.
This is a constant and this time I'm going to create a new future car object like that and then my new vehicle, as you can see, now it flies well and it has Drive and it has top speed. Now let me ask you this question, look what I can see the similarities between the car and the car of the future, they both have the properties of a basic car, like top speed, and they both have this driving function that is very similar, so In fact, it is actually identical, so this is quite redundant, right, there is a lot of redundancy between these two class definitions, so this is where inheritance or subclasses comes into play and is very useful, so what inheritance gives us allows you to do is say that a class inherits from another class and essentially what that means is that if the future car inherits from the car, it will inherit its properties and functions, so there is no need to redefine them within the future car, so let me show you what I want to say here, in the future car let's remove this top speed property and remove this driving function and what I'm going to do.
Instead what I do is make the future car inherit from car and the way to do that is after the class and then the name of the class you put: like this and then you put the class it inherits from, so in This case I would. but car, so in this type of relationship we have car at the top and we have future car at the bottom, inheriting all the properties and functions of the class car. In this case the future car would be called a subclass of car and the car would be called the superclass of the future car and some people might call it the parent class so the car is the superclass or the parent class or maybe just the father of the automobile of the future.
Let's go back to our playground and take a look at what this means for our code here. So you can see that my ride is still a car object and it still has top speed and it still has Drive, but look at my new ride, it's a future car object and remember that inside the future car we just defined a fly. I haven't defined anything else here, but if I type in my new ride and hit the dot here and let the autocomplete do its thing, you can see that the car of the future actually has a driving function and it has a top speed, so you know Yes, I do.
The maximum speed, you can see it is 200 and if I drive, you can see that now the future car or my new vehicle also drives at 200 and also my new vehicle can also fly well, so now it is flying, there is actually a problem here because, right? Now my car and my future car both drive at the same speed, but remember that future cars are faster, or initially I wrote that the maximum speed was 250, so in this case what we can do is do something called override and that allows you to take a function or a property of the parent class or the super class and redefine it essentially that's where the word comes fromoverride, so you are overriding the major version of that feature and providing your own implementation or your own version.
Of this, let me do it here so you can see what it means, so we use the override keyword like this and then we have to declare the unit exactly as it is in the main class, so what we'll do is say print leading to and instead we're going to say top speed plus 50 because this guy is going faster, so now you can see that when I call this guy what I call future car driving, he doesn't use the main classes driving feature, he uses his own overridden definition of what is unity and this is the code that is running here and you can see it is driving to 250 so that is an example of overriding to provide your own custom functionality now inside this overridden method there is a way that I can still access to the main functionality and you can do that using the super keyword, so let me show you what I mean inside this overridden unit function.
In fact, I can access the main class, the definition, all the functions and properties there if I type. super dot and then I can access unity, so when I do this, super refers to the parent class or the super class and this unity function is actually calling this guy here, so in this case it doesn't make sense, you can see it in the console. When I type Future Car Drive, you know it calls the original definition of Drive and then prints this. You know your own definition of Drive, so you know that in this example it doesn't really make sense, but sometimes when you're building your apps what you want to do is add functionality and not override functionality if that makes sense, so you don't want replace what's in the parent class, you just want to add to it and you can do that if you know you can. call the original definition and then you can add your own code before or after that to provide additional functionality, so in this case maybe just change the declaration here and the rockets increase to 50 or something, so let's see now.
You can see this is my original car right when I call Drive it says drive at 200 and then my future car drives at 200 and it's doing something extra and this is how this overridden function works so this is a simplified example of how they work subclasses, but that's essentially how it works and it's very prevalent in the Swift


language, as I'll show you in the next lesson, as you can see, inheritance allows you to save a lot of work by taking what's already there and then improving it. or give you alternative definitions so you don't have to redefine everything again, but can take advantage of the classes you already have.
Okay, thanks for watching, help me continue creating more videos for you by giving this video a try. greetings and subscribing to the channel hello, welcome to lesson 11 of the learning


series for


in this lesson. I want to introduce you to UIKit, it is an Apple framework that contains many of the classes we will need to build iOS. apps, so let's dive in and see what's available in the UI Kit. As I mentioned in the introduction to this video, the uikit framework is essentially a library of classes that Apple gives us to create applications. When you think about it, there are many. of elements common to any type of application, for example, applications can have views, they can have buttons.
You know that all applications will have things that you need to present to the user. Applications will need to handle user interaction on the part of the person using the application. and so on, so developing that functionality every time you go to build an app is extremely tedious and not to mention complex and complicated, which is why Apple has provided us with a UI kit for us to use and it contains a bunch of resources. classes created electronically so we can handle all these common things, so here I am looking at the UI Kit Reference Guide which contains a list of all the UI Kit classes that are available for us to use.
I'll link to it in the description below the video, but if you want to search for it yourself, just go to Google and type in Apple's spatial UI kit, don't just type in UI kit because the first results for that query are not Apple UI Kit, so just type in Apple Space UI Kit to find the right one in the first result, it was also essential to learn about inheritance before telling them about UI Kit because Many of these classes inherit from each other and are built upon each other, so they are not redefined. things and this reference guide used to be organized in a hierarchy so you could see which classes inherited from other classes, but since they changed it and made it as a list, you don't understand that, but I did a Google search and found a statistic of image.
I'm sure this isn't the


UI kit because this image was from 2012. I think so, you can see here in the URL that it's from 2012, but it gives you an idea how. the classes are organized so that you can see at the top of this tree if you can imagine this left side is at the top of the tree and this right side is at the bottom if you take for example this uibutton class which represents a button that the user can touch on the screen The UI button inherits from the UI control which inherits from uiviewcontroller or granddaddy and what this nsobject class does is provide that basic functionality that allows you to create an object from a class definition, so that's something we did through the first part of the classes lesson, this NS object class gives you that functionality and if we take a look at what was next in the chain, then the UI response class, this class UI response inherits from nsobject means it contains that base which will be necessary for you to know all these UI elements or UI elements.
Okay, next is a UI view, so the UI view inherits from the UI responder, which inherits from nsobject, so the UI view gets all that functionality. and on top of that, the UI view class provides functionality to display something in a view, so a UI view is something that you can show to the user so that it contains all that functionality and code and then in that hierarchy down to the button from UI we have UI control, now you like the control to contain all of that functionality before following this line here and in addition, it contains basic code and functionality for a user element control, so things that are specific to show in a view that handles the user in action and events and respond, then we have a specific type of UI control and that is the UI button, which is self explanatory, it looks and behaves like a button with certain events button, so that's just an example of how you know the route that you can see. there are a ton of classes and there are even more now, so as long as you go and know that before you go and do anything with your app, you can probably leverage something from the UI kit to build off of it instead of building something from scratch, so in this video I just wanted to give you an introduction to the UI kit because we're going to be using a lot of classes from here on out and every time I do I'm going to try to remember to reference this guide or at least link to it so that You can can take a look at these classes, it is very useful and handy to have them at your fingertips because you can click on these classes and then you can find out for this UI button class what kind of functions it has and properties that you can use and leverage and how you can do specific things with the button or with that class and also, I might do another series of videos where we specifically go over different UI elements because I think it would be helpful for beginners to understand how to use them, e.g. like a date. selector or a text field or a slider or switch or something like that, so it could be a separate series, they are not just UI elements, like this video and subscribe to my channel to help the channel grows.
Hello, welcome to learn Swift for Beginners Lesson 12 in this video, we are going to talk about initialization functions for classes. These types exist to make sure that when you create a new object from your class, that object is ready to use, and you can also customize these initialization functions to configure them. Configure the object the way you want when you create a new instance of the class. Let's get started and see what it means. So what I have here is a playground and I wrote a basic class here called person and this person class has two properties right now, it has a name property which I initialized to an empty string and it also has an H property which I set at zero and that is an integer.
Now you learned about classes a couple of lessons back and I learned how we create objects from classes correctly and to create a new person object I would say something like var, let's use the variable a equals a person like this and open and close brackets next to it from the class name like this and here I have a new person object and if I put a dot name on it you can see that it is an empty string and if I put a dot age on it you can see that it is zero, so what actually happens when you create a new person object here and you write these square brackets, these two square brackets don't look like you're calling a function, but actually what's happening is you're calling the initializer function of the person class, but you might ask: we don't have a defined here what initializer function.
Are you talking about let me write it by default? It exists and if you don't customize it you don't really have to touch it, but that initialization function looks like this, it just uses the keyword and it has these two square brackets. and then here, between this, like a function, you can define the code here, this sets up your object, so what happens when you create a new person object with this line of code here is that you're actually calling this function from start of the person class and in here you can write code to customize things, so, for example, I can say name equals Chris and H equals.
I think I'm 33 years old. I think I've lost count, so what will happen here when I create a new person? object, it's going to call this a nip function and it's going to set these two properties to these two values ​​here, so as you can see now, the name is Chris and the age is 33, when I create a new person object, remember how I said that you In fact, we can customize the anit function to configure the object the way you want and we can do that by simply adding some parameters to this init function just like we would for any function that we configure, so for example, I can Let's say I can say n is a string and a is an int so now I have to pass these two values ​​so let me delete this part here and I'm going to create a new person object and now you can see the autocomplete shows me this so to the name passed Chris and for the house int at 33, but instead of sending these things to the hardcoded values, now I can set what is passed here, so that's n and that's fine.
When I'm creating a new person object here, I pass these values ​​that come in through this start function and then it sets the properties of those values ​​that I pass. Now remember if I don't want these parameter labels all I need. What you have to do is go like this and we'll learn this through the functions lesson so you can review that if you forget why, these are the argument labels by putting an underscore in there, we're basically saying we don't need the labels. of the arguments now. While we are on this topic of specifying these parameters here many times and sometimes I catch myself doing this as well, it is that when we pass these parameters, the name and the age, and you intend to set it in these properties here, there is a tendency to name these parameters exactly like the property names because, after all, you name these property names to be descriptive of what was present, so in parameters you might be tempted to name them the same way as well.
Well, what happens is it comes into this. situation where you are trying to set this parameter to this property name but they both have the same name so it is a bit ambiguous, that is where you can use this keyword called self and how would you use it, you would say self dot name is equal to name, so self refers to the object being created, so you're saying to set the name property of the object to this name parameter, this is how you distinguish whether the property name and the name of the parameter are the same.
I would do the same thing here like this, so this age refers to that guy and self age refers to the actual property there. Now another thing I want to point out is that I can't call a person like that anymore because there is no int method like that, whichwhat I can do is I can actually have multiple initialization methods, so if I do this, let's say this is VAR b is equal to this person object, well, this point name b is empty and b point H is 0, because these types have After all, it has not been set.
I'm calling this initializer function here that doesn't set anything, whereas this guy, this initializer function actually sets the name and the age of whatever we pass in. Now there is one very important thing we haven't talked about yet. Regarding init functions, I mentioned in the introduction of this video that int functions are there to ensure that the object is initialized correctly and that all its values ​​are set, making the object ready to use, in this case here for the person class. I declared two properties and I already preset them or initialized them with these values ​​here, so really the net function has nothing to do, even if I didn't do anything, as it is in this case, the object would be ready to use because these properties have values ​​in the next lesson I will show you how sometimes you can do it if you need to declare these properties in that case when you create a new person object the anit function would be responsible for making sure these types have a value because if not all these properties are initialized with values ​​then the object is not considered ready to use so the anit function is there to make sure the values ​​are set and like I said so far you haven't learned how to do that.
Declare these properties right here without sending them the values. I've always set something up for them, but in the next lesson you'll learn how to do that and then we'll dig a little deeper into the initialization functions and talk about how they exist. two types called designated and convenience initializers, etc., etc., okay, thanks for watching and remember to like the video and subscribe to the channel if you haven't already. Hello, welcome to lesson 13 of the Swift learning series for beginners. In this video you will learn about the options and it is probably one of the most confusing things for beginners.
If you've ever looked at Swift code and noticed exclamation marks or question marks in the code, then you've stumbled upon it. options so let's get started and find out what it is so here I have a playground and I have declared a class blog post. In fact, if you've seen episode 9, the first lesson about classes, this will look familiar because it's the example we used in lesson 9 when I first talked to you about classes. Now we declare a couple of properties here for our blog post class and these properties represent certain things about what a blog post might have, for example the title. the body, the author, the number of comments, and actually, you can safely ignore this feature, so I'm going to remove it because we're going to focus on these things here.
You'll notice that whenever I declare a property right here, I immediately initialize it to a value so that the title body author is initialized to an empty string right when they are declared and the comments are set to zero right when they are declared now, this is all great, but what if you really want some of these properties? to be empty, for example, what if I want to know if the blog post has an author or not? One could argue that well, I can check if author is equal to an empty string and if author is equal to an empty string. string, then maybe there is no author, well what if the author is not actually a string?
What if it really is? You know we have another class here and we call it person and let's say there's a name and we'll initialize the empty.string, but you know, okay, so in this case the person is assigned to the author, so you know that the blog post has an author because it has this person object, so how do we distinguish that there is no author for a blog post? Well, you have to be able to declare an author property and leave it empty because some blog posts may not have an author, so the way to do it is, if you remember from a long time ago, maybe lesson 2 on types of data.
I think you can actually specify for your variables the type of data it can store if you don't specify it basically the data type is inferred from what you assign to it, so for example if you wanted the title to be empty you would have to declaring the type to get rid of this is equivalent to an empty string because we don't want to log anything correctly. We wanted to delete this property but leave it empty. I would do it like this. I would know: specify the specific type and then I would do it. put a question mark and that's optional right there, so when I declare this title is empty and this part basically tells us that the data type of the title is a string and this question mark attached to the string data type tells us says it could be null, which means there is nothing or it is empty, it could be null or it could actually contain a string, so contrast this with the body property here, when it is declared it is assigned this empty string, so which will always have a correct value, whether it's an empty string or maybe in the future we assign some text to it, it will have some text, but you know this title property could be empty, in fact it's empty right now as we declared it, so going back to this example with the author, some blog posts may not have an author, so we can't just initialize the author property on a person object because that would mean that all posts in the blog at least have one person attached to the author.
We want this property to be able to sit empty the way we do. Again, we specify the specific data type, we put the question mark next to the data type and we get rid of that part because we don't really want to assign anything to it, so this way we have a blog post class that has a Optional title property has a body that is initialized to an empty string, so at least it always has a value and may or may not have a person you know, it may not have anyone assigned as the author or it may have a person object assigned Since the author and the number of comments will always have a value and start at zero, so you'll want to pay attention now because I'm going to tell you how you should think about these optional properties that we have here and this is kind of a metaphor or an image that comes to mind What I was taught when I was learning this stuff is to think of the title property, this optional string, it could optionally contain a value or don't think of it as a box, it's labeled as a string, but you can't see what's inside of it. the box, you don't know if there's actually a string object inside or it could just be an empty box, it could be an empty gift box if someone, if you've been really bad, gives you an empty gift box for Christmas that's never happened to me , but if so, please let me know in the comments section below, but anyway you can think of an optional property as a box which may or may not contain the actual object, so how can you find out?
If there really is a string object inside or it's not right, you have to unwrap that box correctly, you have to unwrap that gift so you can look inside and see and get to the real object and that's exactly what we have to do now in the code with our optional properties, so let me create a new blog post object here, let me say that the post is equal to a blog post like this and what we're going to do here is say a printed post with the body of the point and not it HE. also hello, something like that, okay, so basically it prints hello because the body is empty, but let's assign it to something like that so we have something like hey hello, that makes a lot of sense because we have initialized the body to hey, when we create the object blog post, it's already set to hey and then we concatenate hello with it, so when we print the post body plus hello, you know you're accessing that property and in your pending greeting anyway that makes sense, so we can't do it anymore. that with something like the right title because it's optional, it's wrapped in a gift box and you don't know if it's empty inside or if there's actually a string object inside, so you can't use it blindly, you have to unwrap it from the box of gift and check if there is a value or not and then use it so before using the title we have to check if there is actually a string object or if there is actually a value inside that optional and the way we do that is by doing something. it's called optional binding okay it's like an if statement so you say if but then you use the word let and if you remember using the lettuce keyword to declare a constant right in fact that's exactly what you're declaring a constant here if. let the actual title be equal to the title and then you open these braces, so what you're doing here is you're testing sorry, not the title.
I'm referring to the host point title because this is the optional property here. What you are doing here is you. you're saying you're testing, you're unwrapping this title property, this one is optional and you're saying if there's a value there then assign it to this constant called actual title and then in here you can use the actual title as the value but if there's nothing inside this optional and if you unwrap it and it's empty it's null, then don't run this code inside, that's why it has an if statement here, basically you're testing to see if there's a value inside this optional if you have to assign it to this constant and then using this constant in here, if there isn't one, it's just going to skip this whole if statement, so here we can safely say that you know, print the actual title and also, you know.
Solute, so there is no value in the title, so it actually skips all of this, but let's say for example here the post title is equal to you, so now you can see when it makes this link optional and unwraps this title because we have assigned something. here you find that I've unwrapped the optional, there's a value that I'm going to assign to the actual title, so inside this if statement we can use the actual title and do this, but if I didn't have this line here, let's comment this out. As you can see, it skips this completely and doesn't crash or do anything like that because we're sure to first check to see if there's a value inside the optional and then use it.
Now there's always different ways to do things like this in the Swift


language, there's actually a way for UM to be a cowboy and skip all this if you don't want to check it and you just want to use the value, you know there's something within what you can do. you can use it, you know you're not going to flag it, you're just going to use it, you can move the post thought title and you put this exclamation point and this is called force unwrapping, so what you're telling Xcode is you. I'm saying hey, you know, I know there's value here.
I don't need to verify it. I just want to unwrap it right away using this exclamation point and use whatever is inside there. In this case, it's me, so here we are. Do you know that we are accessing the title of the post? It's an optional property, we don't care, we're going to force unwrap that and take out that value and use it, you know, if you know there's a value inside. you can do that, but it gets a little dangerous because, for example, if there's no value there and you're forced to unwrap it and you're trying to use the value, there's an error because and this is the error, you get a fatal error unexpectedly. correct because when you use this exclamation point you expect there to be a null value unexpectedly found on unwrapping and a correct optional value which is exactly what I was telling you, you are forcing the title to wrap and you are trying to use it but it was actually null, so which actually crashes and in your app building career you will probably see that this error was unexpectedly found as null so now you know why so the safest way to do it would be to use an optional hook. so I'm going to press command Z now command Z for you Americans I'm going to undo undo okay do something like this and this is an optional link so another way you can try before you use it is just Wow, it's just the old fashioned way. if statement and test if it is null then you can say something like test null you can say if post title is not equal to null then print our title post and as you have tested it is not null in this statement if you can go ahead and force unwrapping and use it well to make it work, you can use force wrapping safely because in this if statement you just tested that it is not null, okay, now the other way around, if you want to check that it is null , post point title is equal to nil is not equal to nil many beginners make this mistake and use an equal sign but an equal sign is for the task remember that so if we are going to test null you will use two equal signs like that one and this one is like optional does not contain value, okay, optional contains value and here for the optional link, you know, it contains a value, so like withcomputer properties.
There are also additional things that we won't cover in this lesson. We'll probably do another video on properties and it's property getters, setters and property watchers, but this is a great start to taking a look at properties, so thanks for watching, like the video and subscribe. Hello, welcome to lesson 15 of the learning. Swift Series for Beginners in this video we are going to review initializers and I will tell you about designated initializers and convenience initializers. Okay, let's get started, so here I'm starting again with the blog post class and the person class and these properties here in the blog post class.
In fact, I'm going to uninitialize some of these properties so I can show you what we need to do in the initializer because I mentioned before that one of the tasks of the initializer is to make sure that all of these properties are initialized and ready to go, so that even if is optional and is considered correct when you declare its properties inside your class, there are basically three different ways to do it, so number one is this one where you declare the property and initialize it with some kind of value right away, so that's this body property here and this number of comments property is equal to zero.
The next thing you can do is declare a property and set it to optional. you're specifying that it could be null or that it could contain a value, but either way you should unwrap the value and check to see if it's null before using it. The third way is probably the most dangerous and that is to use the force unwrap operator, so let me show you what that means if I get rid of that question mark and put an exclamation point there and we also do that next to the author, so what you're saying here is you're saying that the title is basically an Optional, it could be null or it could contain a value, but you're going to leave it unwrapped, so when you go to the title property down here and say post title , Xcode won't have that kind of safe check or warn us that it might be null, it won't provide any of those safety mechanisms and will just let us use it as is so we can assign nil to the title, as you can see here it's null and we can use it as a normal property right.
I don't have to check if it's null or not or rather it's up to us if we want to do it or not, but nevertheless if you know the difference, the flip side is that we make this optional, now there are some failsafes if I do it . just try to use this since this knowing that the actual title is the same and this is using an optional binding so basically we're checking to see if there's a value inside the optional first you know that if something could be null it's probably safer to use an optional value so it forces the programmer to verify things. before using it now, if you set its properties that way, it could still be null or it could contain a value, but it kind of removes that safety check that options provide, so those are three different ways you can set However, you can't just do something like that when you declare a property, don't set it to anything, don't specify that it's optional, or don't specify that it's unwrapped and just leave it at that. in that case, Xcode will assume that the initializer will set them to some values, so let's declare our initializer here as you've learned in the past and in here you can see that let's say author equals a person like that, then Xcode will stop complaining because remember that when we create a new blog post like this, it's actually calling the initializer, so even though these properties here, title and author, are not there. set to nothing, they are not optional, they are not unwrapped, this initializer will be called for sure when we create a new blog post object and in here those properties are set to some value, they are initialized and so at the end of the day this blog post object will be ready to be used, now this initializer here is what's called a designated initializer and what that means is that this initializer function is guaranteed to fulfill those obligations of making sure that all properties are initialized before use, in contrast we can have something called a convenience initializer and what that means is that the convenience keyword is used followed by the initialization method signature, so you could have something like this where inside this initializer I just want to provide a custom title.
Well, you might be wondering if I call this convenience initializer, how will the author be initialized correctly? So what happens is inside the convenience initializer. I call the designated initializer using the self keyword like this, so now and in here, let's set the title equal to the custom title, so now when I declare a new block post object and let's say I use my convenience initializer like this and I pass a custom title like this, it calls this convenience initializer, but this convenience initializer also calls the designated one, which ensures that at the end of the day you know that the uninitialized properties will be initialized and then after calling that designated initializer , we set the title as custom title, so whether you call this designated initializer or the convenience initializer, the title and author properties will be guaranteed to be initialized, so the convenience initializer function is simply for convenience, that's the difference between a designated initializer and a convenience initializer hello, welcome to learn Swift for beginners, lesson 16, today you will learn how to manage a collection of data in what is called an array if you are working with many pieces. of data, it would be difficult to manage it simply with constants and variables, so let's take a look at how a career can make our lives easier.
Okay, let's start now. Arrays are one of the three types of collections that are available in the Swift programming language to us. To use and manage our data in this language guide, you can see a diagram of this matrix on the far left. We'll come back to this diagram in a second, but first a definition so you can think of an array as a collection. of data sorted by indexes now, if that doesn't tell you too much, let's jump right into the quick play field and I'll show you exactly how beneficial they are and also how to declare them and how to use them, so first.
I'm going to delete this default variable here and we're going to create a couple of variables ourselves, so let's say VAR a equals dog, VAR b equals can't or C equals bird and now let's say I wanted to concatenate or add the word my in front of each of those values ​​there I would have my dog, my cat and my bird, so I would have to do something like this, I would have to go to is equal to my space plus 8 and this would result in being my dog ​​because We are adding the word my with space a a and a is dog and then we reassign that result to a again, thus overwriting what was before, so now a is actually my dog.
I would have to repeat this for cat and me. I would have to repeat this with bird. I can't even use what we learned in the previous lessons regarding loops to make my life easier. You would have to write this three times for each of the variables, so this is the perfect opportunity. use an array to organize this collection of data, so to create an array with the data that is already in it, we will open angle brackets or square brackets if you prefer and inside these two square brackets we will put each piece of data separated by a comma , so we have dog, we have cat and we have bird, so like this we have an array with three pieces of data and if you remember what I said in the definition that arrays are a collection of data organized by indexes, what do I mean by that ?
Well, you can see that there are three pieces of data here, so there are three distinct points. You can think about it, the leftmost point here, the beginning is index 0 or point 0 if you prefer to think about it. so the next is index 1 and the last is index 2 so the arrays start at 0 and because there are 3 elements here the indexes go from 0 1 to 2 and now if I quickly reopen that language guide, you can see in this diagram, in this array there are five elements, so the index is 4, 6 eggs is 0, the index of milk is 1 and so on until we reach 4, although there are 5 elements because it is zero based.
Okay, so let's go back down here. So it's great that we don't know it here, but we need some way to reference that array, so actually what we do is we create a variable, let's call it D and we assign this array or this collection of data to the variable D. so now if I wanted to access dog, for example, I would type D and then I would type square brackets like this and between the square brackets I would put an integer that represents the index of the element I want, so let's say I want dog.
I would put 0 so you can see here. I would get dog correctly and so we can print it and that would print dog down here now if I change the index to 1 then I would get cat. Now let's do an example where we have something. so just to duplicate that I would say let's say a equals my plus D 0 so I can do B equals my that's my cat and I would finally get my bird, but then I mentioned that there was a better way to do it if Leveraging what we learned In the previous lesson on loops, we can take a look at using for loops and simplify our work here, so remember that in for loops we will repeat a piece of code a specific number of times and you can see here that I am working with index 0 and X 1 and X 2, so this becomes really easy.
I can say remember that the next part of the for loop is a counter so you know that it is my variable that contains the current index and then you write. and then you write your range so I can write 0 0 sorry, I mean 0 2 and this is going to go from 0 to 2, so I think you can see where I'm going. What I'm going to do is print. my plus D and inside here where I put the index normally I'm going to put the counter and you can see here, it took a second to the playground, but that's exactly what I was hoping to do here, so in the first iteration of this counter of for loop is 0, that's the starting range, so 0 is passed here and it would print it. this is dog D at index 0, right, its dog in the next iteration of the loop counter is 1, so I'm actually accessing index 1 of my array D, that's why I get cat and finally a loop again and the counter is 2 and you would access this bird index here.
I want to show you another way to use your for loop with an array and that is simply saying for item in D so what this is going to do is loop through all the elements in the array D and in each iteration of the loop it will take that element or that data and it will assign it to the element so you can just do this so you can see that it prints again in the first iteration the element is dog, in the second iteration it is cat and in the third it is bird so this is a pretty way simple to write it and you can see it saves a lot of work by doing it one by one like this and one by one like these arrays along with loops really powerful stuff now with a race there are other cool things you can do let me make some space here maybe Yes you remove these things you can actually declare an empty array so it's an array that won't contain data at the beginning and the way you do it is like storing things in a variable or a constant array that can only store data of a certain type of data.
You specify it, since it's an empty array, how would you do it: you open two square brackets, you put the data type inside the two square brackets and that data type represents the data type that the array is going to store, so I'm going to put a string here and then you'll end up with two square brackets like that and so now E refers to an array that is empty right now and does not contain any data with the intention of storing data of type string in this array now. If you're going to create an empty array like this, you better be able to add data to that array properly, so what makes arrays really useful is that you can add and remove data from that collection so I can add or remove from this collection correctly. here I can add or remove from this collection here let me show you how to do it there are a couple of different ways I can do something like this D plus equals 2 angle brackets like this again and let's say I wanted to add Mouse and now my array D would contain 4 elements as you can see here dog cat bird and mouse in fact I can even add two data at once comma and then here I could put owl for example to add Mouse and owl to that array so now my array has 5 elements from 0 to 4,the right one starts at 0 1 2 3 4.
Make sure you don't forget the plus sign here because if you do that, you're basically creating a new array with these two. elements and you're assigning it to D and you just lost this data here, so more equals to add elements. Now, seeing this plus equal, you might be tempted to use less equal to eliminate elements like this, but that doesn't really work. unfortunately you can't remove elements from the array that way, what you have to do is the array actually has functions that you can call on to remove elements, so you would say D and then press dot on your keyboard or the key knitted. and you get a list of functions that you can call on this array and using the add function will do exactly the same thing plus equal, it will add elements to that array, but let's look at the delete functions to delete all which is going to delete all the elements in a write, but you can use this here to delete on and you can specify the index of the element that you want to delete, so if I put 0 like this, it will delete the dog from my array, so now it's just going to contain cat bird mouse and owl now, right? what if I don't want to completely remove the dog, but I just wanted to change that element right there at index zero, so let me get rid of this removed line?
You see, you can access the elements in the array by doing that correctly by placing the index there, you can actually change the element, you can change what is assigned at that index by writing d square brackets placed at the index you want to change and using the equal sign to assign something new that place, so here let's say turtle and that will now change its array if I print D oops zero and we want to get turtle instead of dog because I just changed it here. The last thing I want to point out is that with arrays you can also check how many elements are there if you look at the count and that will return the number of elements in your array which is sometimes useful when you want to use a for loop with a range and you don't know how many The elements are in the array, you can use the point count of this array and get this number here, but note that although D has five elements here, the index of the last element is actually only four because the first element is zero, goes from zero. one two three four right, although there are five elements, so keep that in mind if you're going to use this array point count in conjunction with a for loop or something, okay, that's where we'll end up with the arrays.
As you can see when you type array dot, there are many different functions with arrays that you can do. What I've covered here in this lesson is enough for you to use arrays and take advantage of some of the main benefits of arrays as we go. and we're building applications together, you'll learn new ways to use arrays, but for now, these are the main things you need to know about arrays to start using them. If you like this video, give this video a try. approved, subscribe to receive more. Hello, welcome to lesson 17 of the Swift learning series for beginners.
In this video, we will look at another type of collection called a dictionary. In the previous lesson we looked at the array which you can see here on the On the left side and with an array we had a collection of elements where the order mattered, so you can see in this example here in the Swift programming language guide. 6 eggs are at place number 0, while bananas are at index 4. Alright, a dictionary on this right side here is a type of collection where the order doesn't matter, so in arrays we retrieve the element by this index here and with a dictionary because the order doesn't matter, we retrieve these values ​​using a key so that each value has a key associated with it when you put it in the dictionary and you have to pass it the same key and it will return the value.
Now, the type of collection you use to organize your data will obviously depend on the type of data you are storing. Order matters, if so then the easy answer is to use an array, if not then maybe consider using a dictionary, so this is a good example. Airports have these correct airport codes, so each airport has a key associated with them and that's a good point. the keys should actually be unique for each value that you put in another great example if you use a dictionary and this is the example that I'm going to use in this video is license plates so for example each license plate is linked to a The car and each license plate is unique, so the key can be the license plate and the value can be maybe a description of the car or something like that.
Let's jump into Xcode in this new playground I have here and let me show you how to declare. a new dictionary and how to work with it, so why don't we declare a variable here and call it cardi B to represent the card database? It looks like my playground has crashed and what we're going to do here is declare a new dictionary so we use the keyword dictionary and followed by that we have these angle brackets inside which we specify the data type of the key followed by the data type of the value so, for example, let's look at that example again here in In this dictionary, the key would be a string and the value would also be a string, so between these two angle brackets we'll just put a comma string and To create a new dictionary object we would simply end up with those two square brackets and so we have an empty dictionary that stores key-value pairs and a key-value pair is just a fancy name for one of these data pairs.
You already know a key and a value. This dictionary stores key-value pairs where the key is a string and the value is also a string, now there is an easier way to write this without having to type as much. Let me show you the second way var car DV, let's say DB 2 is equal to using the square brackets and then you specify the data. key type followed by a colon and then the data type of the value and again we have these two parentheses here to create a new instance of that dictionary or a new dictionary object and that is equivalent, these two are the same, doesn't it look similar this to declare a new array?
Don't be confused, for example declaring any array of strings would look like this would be my empty array expecting to contain string objects and this is an empty dictionary expecting to contain key-value pairs where the key is a string and the value of the chain too, okay, so we'll continue with this. Kind of a statement here, so I'm going to continue by removing this array example that was just for demonstration and removing it, so now we just have cardi B is an empty dictionary. Now how do I assign something to the car database dictionary? Well, I would. cardi B and then I would use these two square brackets here and pass in a key or specify a key and this key would be a license plate, so you know this is going to be different depending on where you are in the world, but let's say it is like this and then you assign the value in the dictionary for that key, so this would be, say, a blue Ferrari.
Now this value of the blue Ferrari is linked to this key jsd two three eight, how do I retrieve the value properly? It's very simple, I just give it the key, so if someone were to search for this license plate, let's say I print this like this, it would print a blue Ferrari, but notice that it's wrapped in an optional tag because, for example, if I passed a key that doesn't exist , let's pass as ASD two three eight, then you can see that there is no value for that key, that's why it returns nil, that's why when you access a dictionary and you pass a key, it returns an optional data type, whatever its value is , so when you pass a key to your dictionary to retrieve a value, just expect it to be optional and you may need to unwrap it and check for null before using it.
Okay, what if I wanted to tag this? Before continuing, we declare a new dictionary, add key-value pairs and retrieve data, and how do we update a value for a key? Well, it looks exactly like this. here for you to specify the key that you want to update the value for and here we can say that this guy is now a red Ferrari, maybe he got a paint job or something, so when you pass this key from now on, you're going to get this new value because this basically overridden what was before and to remove a value remove a key-value pair, let's say you can do something like this, you pass the key to 3/8 and assign it zero and actually that's I'm going to remove the key-value pair from your dictionary.
Now I'm going to show you how to iterate over all the key-value pairs in your dictionary, so why don't we add a second key-value pair here so that we have more than one element to display, okay, and this can be a green Lamborghini . I think that's how it's written. I don't have one so I'm not sure and now let's iterate on it. I wish I had a window to iterate it. use a for loop operation so we can essentially say for each key-value pair within the dictionary do something and the way you specify this is by passing we use what is basically called a tuple okay so you can think of a couple as a set. of variables or a bunch of variables, so let's say car license in cardi B.
Now the in keyword shouldn't be new to you because you learned about the for loop in a previous Swift lesson, so basically, what However, it should be new to you. This is a tuple, so for every tuple in this dictionary we can do something. What will happen is it will take each key-value pair and the key will be inside the license and the value will be a car like this, so now inside this for loop it's going to iterate twice and I'm going to find this key or this license inside this license variable and I'm going to find the car, this string here, blue Ferrari or green Lamborghini inside this. self variable and it knows it, although there is no data type associated with this tupple because my dictionary is a string string for the key and the value.
I'm just going to print a car like this and you can see that hmm, it's just printing a key. value pair here is printing a car, but I have two elements and the reason is because we've actually removed a key value pair with this statement here, so if I just comment out this guy, we have our two cars and it's a red Ferrari because I changed it here. I can also print the license. I can say, "You know, license." I can say something like Carr has a license, so he has a license, so he basically ends up using a dictionary and you'll find it.
It will be useful along with side arrays to organize your data, so thanks for watching. If you like this video, please like it and share it with anyone you know who is also interested in Swift. Thanks for seeing it. See you next time. Hey, did you join my free Facebook community? That's where I come out along with a bunch of other people learning iOS like you. I also post early access to all my videos within that group before I post them to YouTube. You can also get help with any questions you have visit the link below click the join group button and I will approve your request right away so I will see you there soon we will talk to you

If you have any copyright issue, please Contact