Swift iPhone Development for Beginners (2021) ✅ | Afraz Siddiqui | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Swift iPhone Development for Beginners (2021) ✅

teacher avatar Afraz Siddiqui, Engineer | Designer | Teacher

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

23 Lessons (3h 30m)
    • 1. Swift for Beginners Part 1 Getting Started

    • 2. Swift for Beginners Part 2 Variables & Constants

    • 3. Swift for Beginners Part 3 Types

    • 4. Swift for Beginners Part 4 Functions & Parameters

    • 5. Swift for Beginners Part 5 Classes & Structs

    • 6. Swift for Beginners Part 6 Loops

    • 7. Swift for Beginners Part 7 Optionals & Unwrapping

    • 8. Swift for Beginners Part 8 If Else Conditionals

    • 9. Swift for Beginners Part 9 Guard Statements

    • 10. Swift for Beginners Part 10 Enums & Switch Statements

    • 11. Swift for Beginners Part 11 Protocols

    • 12. Swift for Beginners Part 12 Strong vs

    • 13. Swift for Beginners Part 13 Closures

    • 14. Swift for Beginners Part 14 Strings

    • 15. Swift for Beginners Part 15 Arrays & Dictionaries

    • 16. Swift for Beginners Part 16 Ints, Floats, Doubles

    • 17. Swift for Beginners Part 17 Libraries & Frameworks

    • 18. Swift for Beginners Part 18 Naming Conventions

    • 19. Swift for Beginners Part 19 Ternary Operator & Return Statement

    • 20. Swift for Beginners Part 20 Typealiases

    • 21. Swift for Beginners Part 21 Math Functions

    • 22. Swift for Beginners Part 22 Searching & Sorting

    • 23. Swift for Beginners Part 23 Wrapping Up

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

In this this course, we will learn what Swift is and the fundamentals of the language. All of what we learn can be applied to iOS / iPhone App Development. Topics we will cover include Classes, Structs, Variables, Constants, Functions, Loops, and much more.

This course is for ABSOLUTE BEGINNERS! No prior programming experience or knowledge is required to get started with any of the lessons. To get the most out of the course, I encourage you to follow along with us in a hands on fashion. I also encourage you to post your questions and feedback for your own help and for others to see.

Meet Your Teacher

Teacher Profile Image

Afraz Siddiqui

Engineer | Designer | Teacher


Hi there, I'm Afraz!

I've been writing code, building awesome software, and designing creative projects for over 12 years. If you are interested in

iPhone or Android App Development Website Development / Design Cloud Computing Photoshop / Adobe Suite Tech In General, Just Improving Yourself

you are certainly in the right place!

I have taught hundreds of thousands of people just like you how to succeed in a variety of disciplines through actionable content, interactive feedback/help, and straightforward walk throughs. With a background in technology and entrepreneurship I have successfully navigated several complex challenges and learned a lot along the way - now it is my time to give back.

I am currently leading Software @ Microsoft and previously comp... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Swift for Beginners Part 1 Getting Started: folks, welcome to your first lesson in Swift for beginners. My name is a frauds, and I wanted to take this first lesson as an opportunity to briefly introduce myself and provide an overview of what we will be covering in this course. So what that being said, um, as I mentioned, my name is a fraud, as I am a software engineer at Microsoft, focusing on IOS and, respectively, Swift. I have been in the industry for about 10 years now. I created my first iPhone app back in 2000 and nine, which is ridiculous. I think about now. I have published several abs of the absolute over 80 avoids. Around 20 of them have reached the top 10 charts. And, yeah, it was swift. For several years in this court's, we're gonna be covering Swift for absolute beginners. No prior software programming experience necessary whatsoever. Um, we will be going over a variety of topics such as variables, Constance classes, function statics, basically all the language features that are used professionally on that you'll find most common on a beginner scale. I will say there are several other features that do exist that are not used as commonly, but we'll learn how to learn what we don't know. Towards the end of this course. The way that we will be progressing through this course is by explanations and examples. This is how I personally learned as well as taught others. So with that being said, um, let's set up our environment on get some kind of housekeeping stuff out of the ways you can get rolling in the next video with some code. So first and foremost, you will need a Mac to follow along with this course as X code. The tool that we will be using to write our code is only available for Mac by apple. So if you have a Mac, keep on watching um, the second thing we need is X code. So exclude is kind of like an editor. Well, it is an editor where you can write code and particularly ex code is very good for Iowa's objective Seen swift. So what you're gonna want to do is open up the APP store on your Mac and appear in the search bar. You're gonna want to search for X code, and of course it's free and you'll see it right here. It's developed by Apple. Some cool screenshots here. Um, so you're gonna want to download it, install that. It's quite large, as there is quite a bit of tooling and here to help you develop pretty fast. So I would recommend having about five or six gigs free on your computer to install it. It takes on a little bit of time to install. Yeah, once it's installed, what you're gonna want to do is open up, exclude. And we are looking right here at something called a playground. So playground is not an application that you create like a project in excluded, but rather what it is is ah, like a little space for you to play with Swift. That's the name playground. So you can write code in here and you get a preview of what it does here. So what you gonna want to do is do file and new bloops You can actually, when you when you opened up by exclude, you will see the following window. You'll see this welcome window. Um, and I've already clicked through this. We're going to basically click create new playground and once you click, create in your playground you're gonna be presented with this view And this is where we're gonna be demonstrating all over examples and kind of writing code and going through it, I'll be providing all my resource. Is that using all the code that we write and downloadable files for you to follow along and reference material? What I also want you to do is make one quick edit in here for your environment, which is gonna help us see a real time preview of what we're writing here on the right. So whatever we type here, we'll see the preview here. So what you want to do is go down here in the bottom left and you want to click and hold on this and you'll see this menu and make sure you have this automatically run option selected . And what this does is every single time you write a new line of code, the editor will interpret it real time automatically and show the output here. So if you're not really keen on understanding what this is currently doing, don't worry about it again. This is a preface lesson. So yeah, that about does it for this video. I'm really excited to be making this swept for beginners cores. It will really help you get the foundations down to gettinto IOS development to get a professional job in I A less, um and really learn the foundations of fundamentals that I think a lot of software Debs and engineers overlook. So with that being said, welcome again, uh, leave a like and comment below If you have any questions or are excited for this course and I will see you in the leader lessons, Theo. 2. Swift for Beginners Part 2 Variables & Constants: welcome back to lesson number two in Swift for beginners. In today's lesson, we're gonna be mainly focusing on variables Constance and Statics. Now, before we get started on those three particular things, I wanted to talk about what Swift actually is. For those of you who don't already know. So Swept is a programming language created by Apple back in 2014. And it was meant to replace Objective C, which is an older language from back in the 19 eighties. So the reason it was kind of made waas, too modernize the language and make developing a little bit more fun. Um, kind of like get rid of the verbosity of the old language at some, like newer modern features and just make it more of a pleasure to develop eso Swift is used mainly to create IOS, abs, Mac OS, APS, apple watch jabs, some or most of the apple ecosystem. It could be used in other places, like back end of servers and things like that. But for the sake of this video, I suspect the most of you are interested in I West Development, which is why you're here, which is awesome. Yeah, that's kind of what Swift is. And in today's video, we're gonna be starting off with variables, constants and statics. So what the heck is a variable? Constant and ecstatic. So let's just put this up here to get started in type today. Cool, so variable in a constant are very similar. So a variable is a is basically a thing that you're a sign into another thing. So what does that mean? That's the most basic definition. So here we have this variable that we have called str, and it points to the string called Hello World Right, So a variable can point to two types of things. A primitive or or an object so primitive or things like strings like this here a number, um, a decimal point number, which is called a double. And things like that. Objects are things that look a class that models things in the real world, so we might have an object that represents a person. So ah, variable John could represent a particular person, a person. A no object can also be. For example, let's take the Facebook app. An object can be each of the Facebook posts in your news feed, so Let's see this an example. So we have this variable Hello world. Let's create a variable called X and make four. Let's create a variable called why and make it one. If we wanted to add up this foreign one, we can now do X plus y So the variable actually is your your instance of whatever you're assigning to it Whatever you call it goes on the left of the equal sign and whatever you assign it to goes on the right of the equal sign. Cool. So now the other important thing about a variable is as the name implies, it can vary so a variable. Even though we said why appear is one later on in our code? All we can do is we can see now why equals two. So if we were to do X plus one, here are sorry X plus y here we would get five, but if we did X plus y here, we would get six. And the nature of a variable is you can change it for the sake of it. Varying so an example and practical usage would be if you're liking a post on Facebook. So initially it might have four likes, and then once you hit the button, it might not have five. So you want you want to signify that in your code by holding that number in something that can change, which in this case is a variable, so that differs from a constant, which is again very similar. But the difference is the Onley difference is that a constant, as the name implies, cannot be changed. So now I can't come down here and say this now equals 12. You can actually see an error on the right hand side of your screen, saying that you cannot change this. It is immutable. In other words, you cannot mutate it. So if you want to change something a variable later on in your code and just anywhere in your program, it needs to be variable now or ever will go away. There's red highlight line. If you give it a second there it goes, and now this is good to go. This is sin tactically correct, and Swift is going to know what you want to do. So now that we have variables and constants, what's a static? So a static is very similar to a constant with one key difference. That key difference being that a constant is something that you create once. But if you create multiple copies of the file or the object that it lives inside of you create multiple Constance. So I'm gonna show you an example to explain what I mean. So we talked about how an object can be a person, so an object is a class. So if we create a class called person on don't worry about understanding this concept of a class, we're gonna have a separate video on this. But just for the sake of understanding statics, we have this person here and we can create an instance of it by saying var John equals person. So then we have John, which is a type of person. So where does our static thing come into play? Let's say we wanna have one case, um, for a person named Dave in here, we can make a static variable rather a static constant called Dave, and it can equal date like an instance of a person. Let's just make sure that this isn't tactically correct. No errors looking good. So the benefit of doing this now What we can say here is we can say Let Dave equals the person dot Dave So it might complain because we don't have everything outlined. Uh, appropriately, however, lips, I actually made a mistake. We can actually not even include the parentheses, so a static can be accessed without creating an instance. So again, if you're not too familiar with the class or familiar at all, don't worry at all. But for the sake of his video of what I really want you to understand, what is what a static is? So let's recap a little bit. So variable is something you assigned to something that can be changed. A constant is something you can assign to something but later cannot change right so we can to do this line. A static is similar to a constant, but the difference is if we create multiple person objects, every single one will have a different constant inside of it. If it's just a let. But because we put this static keyword, we will only have one copy of one version of Dave throughout our entire application. So wherever we say, person dot Dave will just have one instance of Dave So here we created John and we have. It's an instance of a person. So imagine if we created a property in here called, um, name equals. Let's just go with Smith. Less of this is last name, right? Let's say let's make it a variable And what we want to actually do here is if we create multiple of these, let's make this Jen and let's make FIS Brie. And let's make this Tim each of these people. Each of these variables, in this case, are instances, a new instance of a person, and each of them has this differing last name variable inside. And even if we make this a constant by putting a lead ahead of it, each of them has a copy of this last name. The difference is if I save john dot dave, which maybe isn't the best example, let's make this, um, let's make this an age, and let's say this is 12 right? Every single person. If I get their age, it will be 12. And the beauty in the power of this is looking a practical example. Let's say you only want one thing to ever exist for a particular object you can make that happen through a static. So practical example is in a lot of Iowa saps. I'm sure you've seen in APP purchases where the user can buy a add or remove something through your app. So there is an object that handles all of the transactions and payments and credit card processing, and that object is called the transaction Que So For the sake of this video, just understand there's this thing called the transaction que Now imagine if you the person taps by on multiple buttons and you're selling multiple things. You want the transaction que to present those payment confirmation windows in order. So instead of creating multiple transactions at the same time and multiple pop ups coming up in the person getting confused like hey, what am I being charged for? You want only one transaction Que. And the transaction que object is actually written by apple. And if you if you kind of guessed the transaction, que is a statically created object, and the reason is is because we only want one thing processing transactions now, things like a cute up, which is why it's called a transaction acute in a linear way. that one fingers after another. But that's the beauty of a static. It's their only once for the sake of the usage for that item. So let's let's ah, let's recap a little bit. And let's show a couple more examples so we can create a constant of Let's say we want to call it Price and we can do 1.23 right, $1.23 or whatever your local currency is now, this can't be changed. Um, let's say we want to create a variable called name and let's say, Joe, and let's say this is a current person who's like, signed in tow are at before APP. Has any, like, signing mechanism toe log into an account. Once the person signs out on a different person, signs and weaken. Say, now, name is Jill. We're sticking with the J names today, So in essence of variable and a constant there, the building blocks of everything in your code so we can ignore this complaint For now, um, off of person is complaining because we don't have a Dave. We could make this stage, and this will go away this error, But, um, in essence of variable is something that points is something else. It can vary in its assignment. The assignment is the thing that goes in the right hand side and the name of your variable , which is also called an instance. Is this thing on the left inside? This is the name that you give it. You can call it whatever the heck you want. Generally, of course, you want to call it something legible, readable and kind of understandable. Um, a constant. Whereas this is a variable, a constant is the exact same thing, but the value cannot be reassigned. Once you set it to something, you're one and done. You can use it. At that point, you can get it, but you cannot set it again. So in this case, we can say this is a Let's say, this is your last name, right? You cannot change this again. If you want to change it, make us a variable. Moving back to statics. A static is very similar to a constant where it cannot be changed. However, a static will only give you one instance of whatever you're trying to grab from a class from the object. One time in their app, and I think it's static is on. There is a little tougher toe wrap your mind around. Um, and I think we'll see this MAWR, but better explained and through examples and are later examples that we do in later concepts we cover but again to give an example of the transaction. Q. If the were charging ah, user transaction like a payment, we only want to have one thing processing transactions, and that would be an example of a perfect candidate for something that we want to make static. And another thing that I may have actually forgot to mention. A static needs to go inside of a class, so it needs to be a part of an object. You cannot put a static outside of a class like we did for variables and constants. It'll actually complain that it will say that it needs a type which is maybe not the most most helpful of errors. But what this is actually saying is it needs to be inside a class and classes represents an object. We're not gonna be worrying about what a classes per se in this video, but just know that a static needs to be inside a class which is synonymous to an object. And this is how we would actually get that static out So we can say, um, the age is on person dot age So you can say person is the object. We want the dot. Whatever the static constant, it's a constant is in here. This is how you actually create an instance of your person class again, we don't need to focus on that too much. But just for the sake of explaining what I have written here and yeah, these are just some examples of a constant and a variable, and that about does it for this video, I will be providing some resource is and some hopeful us hopefully of some assignments in the discussion area or comment section below. So I encourage you to really, really, really understand these two concepts other three concepts of variables, constants and statics, as they are truly the funding fundamental building blocks of everything and swift that we're gonna be doing so that being said, I hope you found this video informative. Please, please, please leave feedback below. If you have any suggestions whatsoever, leave a comment alike. Follow. Um, whatever else you can do, please do. And I will see you guys in the next lesson. 3. Swift for Beginners Part 3 Types: back to your next lesson in Swift for beginners. In the last lesson, we went over variables, constants and statics. And in today's lesson, we are going to be going over types. So we're actually picking up exactly where we left off in the last lesson. So let's actually start by getting rid of all of this. So let's select all of this and let's get rid of this today we're gonna be going over types . So what is a type? So, as the name kind of implies is, um, in assignment of what type of thing is so sticking with their prior lesson, let's create a variable is call it X and make it equal to three. So the type would be whatever the type of thing that assigned to X is in this case, it's an integer right. Let's make another variable and call it name and let's call it Joe. So the type in this case of name would be string. This'd be integer, so I hope this kind of makes it clear a type is the actual kind. Really, it actually is the type of the variable or constant or static that you're dealing with in a swift has his powerful language feature called type inference ing. So because we said X equals this number, Swift can infer whether it can guess that. Okay, this is going to be an int right x will be an end and the way we can Ah, check of swift really knows is we can do this type of and put whatever variable in here that we want to check. And if we move over to the right here, um, if it runs, we go. Hopefully we can see the actual type is an end and respectively, if we do that for this variable right here, once it runs, if the string So this is swift under the hood guessing more or less of what the type of these variables are now often times in professional development, you'll see something like, um, it's in last name. You'll see something like this. So this is an example of a variable called last name, and it's explicitly declared to be a string. So this colon Ah, and the thing after here before the equal represents the type that you have explicitly set for this variable to be. So if I come here and try to assign this to, like, to. Our editor will yell at us because we have explicitly said before the equal sign that, Hey, this last name is supposed to be a string so, respectively, it should be a strength. So, um, let's just should just another example. Let's to let age is gonna be an integer and let's make it 22. Um, and I Let's do let Price and Price will be a double. And let's make this 1 99 and so on and so forth. So that's the basic notion of what a type is. And Swift is a strongly typed language, and what that means is, once one variable has a particular type or a constant, has a type for the duration of the entire app running, that type cannot change. So if this last name is a string, we can't come down here and say, OK is last. Even though it's variable, we can't come down here and say this now equals three. It needs to be one thing or the other right. It needs to be whatever the type is and you'll see here that is complaining because last name is expected to be a string. So with that being said, let's take on some mawr examples. So we talked about primitives and objects. Right? So primitive in variables and constants is something like a string or a number or a double , and an object can be an instance of a class. So in the prior video, we had a person class. So I want to do here is go over not only how to create types of your classes and objects, but I also want to talk about a couple common types. So as we've seen here, we have the string very common type. We have a couple different types of number types sewn into is a round number integer pretty basic math integer a double eyes, a decimal point number with up to two floating points. So interviewer with, uh, decimal and two points and then another common one is float. So this could be, let's say, like hyped and someone's height could be, Let's say, if we re learning a nitty gritty and like centimeters, we can do like on a 200 some crazy long number. So this is a floating point. The type is afloat, of course, we can create our own types like we have this person here so we can save our John is a person and notice We just signified the type here. We're just explicitly saying that this is the type and it's important to know that even if we don't include this, this is still correct because Swift can infer and guess like, Okay, we're creating a person here with these two parentheses. And this thing is a person there for John was most likely gonna be of type person, sometimes professionally. When you're actually working with Swift, you're going to run into situations where you get an error along the lines of ambiguous type. Now, I don't know what Apple could have made the error simpler, but in other words, that swift basically yelling at you Ah, and saying, Hey, man, I can't figure out what the type is because you haven't specified it. So in those cases, you might have to explicitly specify after the colon of your variable or constant or even static what the type is gonna be before you assign it. So, uh, going down common type, So a string and int a double afloat, we could have a thing. A variable or constant that represents like a button. And there is a class. There's a framework included in the Apple libraries called you I kit. And for the sake of this video, I wouldn't go over kind of just exemplifying types and not really worry about what you like . It is, um, for brief summary. You I cages holes, user interface elements. So things like buttons and switches and images and anything user interface related, and I want to exemplify that everything is a type, so let's say we have. But in one, it's gonna be, ah, button. I would just create a button. So again, we don't need this year per se because 15 guests Swift is smart enough to guess like, Okay, you're creating a but in here. Thus, it'll most likely be a button, but it's nice toe. Have this explicit notation for somebody else was coming in and reading your code also for yourself. When you're learning and you're starting off, it helps to really know what the types are in. Uh, now, later, all we can now say, but in one equals true, Um, and that brings me to my next type. The next type of just fairly common is bull, so se is complete, so bull is a representation of true or false. So imagine you have a variable that's like is complete or let's say you have Let's say you're have application. That's like a bank account application. You want to open an account, you might have a variable in your entire app. That is, Ah, that no Tates. If your application is complete, so we have is complete and it would be up type Ah, Bull. And initially, it's false. And let's say the user now has finished up the application. We can now say it's true. So with this example, um, it's ah important note that the type actually only goes in the definition of the variable or constant notice. We don't put the type here. We don't do something like like that, right? Swift is gonna yell at us and be like I don't know what this means, So we need to put the type if we're not using type inference ing were swift will guess in the definition, and the definition is either your var let or static the name of your thing. Your variable constant are static. Ah, colon, the type. And then whatever you're assigning it to. So I hope that clears up what types are and better explains as we go through what we're using and why we're using them. And yeah, I will see you guys in the next lesson. Please leave a like some comments below. Feel free to ask for any clarity. Help! Feedback suggestions are very much so welcome and encouraged. And I will see you, Theo. 4. Swift for Beginners Part 4 Functions & Parameters: to your next lesson in Swift for beginners. In the last video we discussed types, and in today's video we will be picking up exactly where we left off, and we're gonna be talking about functions and parameters. So let's start by getting rid of everything that we had done for our last lesson. Make sure you leave the import foundation up here and let's just know Tate this as funks and parameters. So what is the function? So function is a set of instructions or a block of code, which is signified into a group by a function name. Ah, function can take variables inbound. The function can also return a variable. So if you recall, we talked about variables on bacon encapsulate pieces of data. So when we have a function, let's say that, um adds two numbers. Together we can send into the function those two numbers, and maybe the function could spit back out to us. See some of those numbers. So let's create a basic function. So to create a function and swears to use secured funk, you provide the name of the function. So let's say my first function you put parentheses and you put curly braces and you open up a block like so and when you want to add parameters and parameters are the thing that the variables that you are sending into the function you put them in v parentheses. So this is what a basic function looks like at its absolute core basic foundation. Now, what if we wanna have a function that takes in two parameters to variables and adds them together and then returns the some of them? Now, this is where I were. Discussion of types is very important. So let's say we create a function. We say funk add to numbers, and we open up this and do this. So now I'm I mentioned that the actual parameters go in the parentheses and let's color so before even I before I even get to what we're gonna call our parameters. Ah, parameter is an instance of a inbound variable, and you can give a parameter and rather you have to give a parameter a name and a type similar to a variable. So we're going to say are two parameters were gonna be A and B, so A is gonna be a type of int and B is also gonna be a type of event. So if you haven't noticed already, the syntax, the way this right now is very similar to how a variable would be defined. Onda signify that this function will add these two together and have something to return. In other words, a return value. Before this open and curly brace, we're gonna come here and do a hyphen, the greater than sign and the type that we expect this function to return, which will be an int in this case. So this is a function that takes in two parameters of type independent and returns in it. And this is complaining right now because we're not returning anything in here. But in the function signature we have said this function should be returning it. So Teoh access these two variables in this function, we refer to them by A and B, which is why we give them a name. So what we would actually doing here is we want to return a plus, be so a and B because we know those air ends because we've explicitly said they should be ends. We can say A and B, we want to return the some and we're just gonna return. There is whatever A and B sums up to now. But we could also do is do something like var some equals a plus B and then do a return of some. Um, but, you know, keeping things a little more shorthand and a little more legible. We're just gonna do return a plus B. How the heck do you call this function now that we have this function? Imagine if we had two numbers until we have our X is three and bar why is just like a small number to start off with? To exemplify this, we can say, Let's some equals, Add two numbers and notice it. Auto completed it for us, and it wants something for A and B, and in here we can pass in X and why? And if we go over to the right here, you'll see that we passed them these two things and this some comes back. Is this because the instructions that we put in our function of what the function to do is basically take a and B, add them up and return them. So this sum is the sum of these two things. And now, to prove that it really works, we can add some astronomical number here and here. Um well, OK, and it is gonna yellow Does entire Xamax capacity of size. But let's make it something bigger that it won't yell at us about, um So let's take that number and we should sum it up and get the results back on. Like so we get the result back in this Some, um, is correct. So that's the basics of a function now having our understanding of types, we can add endless number of parameters to a function, and we can have any type of return value from the function. Now, of course, the function may or may not have a return value doesn't have to. So let's do a couple more examples. Now we have this. We have this ah, function that does that sums up to numbers, right? What if we want to take this some and we want to find, um, what the exponents would be rather we want to find what the value would be when we raise this number to let's say itself. Right. So if you want to do this times this so sometimes some. So this number raised to the power of to right, So we're squaring it so we could make a function. Let's make it appear to organize this. Let's make a function called square number again. It takes in a which is just the name that I give it, and something important to know is you want to name your variables to be somewhat understandable. So a and B or not the best of examples let's call this number and it's gonna be an ent, and this will return int, and we're gonna say return number, times number. And if you're not aware, this is the this is ah, the multiplication, um, keyword in most programming languages. So that's what the star is used for. And what we want to do here is fixed. This which is why it's yelling at us in terms of case sensitivity and, yeah, So what we're gonna do is we're gonna say, let's squared. Some equals our function, and we're gonna pass in some And, um, maybe I should have mentioned this little earlier on, But to call a function, you put the function name and then parentheses. And if there's parameters. Generally it auto complete for you, but the parameters go in exactly the way they wrote them out up here. So let's say we had a function that just got called like, let's say we have ah, application where we want a log like an analytic for something. Let's say the user tapped on a button. Let's say we have log as a function and there's no parameter is in no return to call it. We could just do this, just log and notice. It highlights it for us. So let's do one more example. Anything we can wrap up after that, Um, let's have a function which takes in a number and it'll return. True or false. It will return true if the number is even and it'll return false. If the number is odd, so we can have a function here is even it's gonna take in a number. It's going to return a bull, and by default, everything is going to return false. And before we do this, we're gonna check if this number is even if it is even will return true before it gets to the bottom of the function. And I'll explain a little bit of what you know, I just mentioned of what it means by return before you get to the bottom of the function with this example. So let's not worry about this if we're gonna be talking about this conditional later on in another lesson, but we're gonna be basically checking if number percent two equals equals zero. In other words, if it's even, if too could be divided into it, return to true. So what do we what we have going on here? So the first thing you'll notice is we have to return statements what the absolute heck is going on? So in a function when you return, if it expects a return type, if you hit a return statement before you get to the bottom of the function, this line of code will never execute because you already called return. If this if this condition matched up and thus the function is like, OK, I'm done. Whoever called me Here's your result. I'm good to go and it will never come down here, so you might find yourself in a couple of cases where you find a multiple return statements in one function. This if condition is basically saying if whatever number we have here this parameter if it if two, can be divided into it evenly yielding a remainder of zero, that's what this percent means. This is a module oh operator that will discuss more in depth in the latter lesson return. True. Um, so let's test this out. So we're going to say, Let results Eagles is even and let's put in four. We should get true hopefully. So we get true here, which is awesome. Well, it's called us one more time. Let's change this a result one and, um, I may or may not have mentioned. Hopefully I have, by constants and variables that anything your making should be uniquely named eyes. Something is named the same thing. Swift is gonna not understand which one you mean. Vessey needs have unique names, but let's pass in an odd number here. Let's go with seven, and that should say false, which it does beautiful. Let's do one more with a larger number. Let's do 7 20 and of course it should come back is true because it really is even awesome. So that's functions at a glance. We've seen what a function is had a call, a function what a parameter is how to create a parameter, how a function can return something how to signify that it will read returning this type of type how you can have multiple return statements and that's about it. Um, I really hope you guys grasped functions in general and what they are. They are really the building blocks some of the variables and constants of programming in general, especially with swift pieces of code organized into functions. You wanna have functions, be very modular, so you can reuse them over and over. An example of that is, let's say you want to multiply two numbers instead of writing that multiplication formula in a variety of places in your code, you want to just have one function for it, and that will take care of it for you. Yeah, I hope you guys found this helpful. Um, leave some comments below. Leave it like let's get some discussions going if you need any clarity or help and I will see you guys in the next lesson theme 5. Swift for Beginners Part 5 Classes & Structs: welcome back to another lesson for Swift for beginners. In the previous lesson, we covered functions in today's lesson. We're gonna be covering classes and Struck's, and this is kind of where things get even more interesting than how interesting they are already. So, without further do you were gonna be picking up exactly where we left off. So let's get into it. So what I want to do is start by getting rid of all those functions stuff that we had going on for our last lesson. And let's title this classes and Struck's. So what is the class and what is instruct? So they're very similar that they also have some key differences. So if you recall in a prior lesson, I was doing something that looks like this. So what the heck is this class thing? So in computer science, and this is a little more agnostic of Swift, and this is a general fundamental, I would say, Oh, computer science and it's applied in Swift is object oriented programming. The notion of object oriented programming came came about in the eighties, and the idea is everything in the world around. You can be modelled as an object so you might have a chair and you might have a table. And both of those things fall under the object category of, ah, furniture and furniture. That zit that French is made out of wood might fall under a category of things made out of wood. So there's a natural hierarchy to the way that things are modeled in the real world. So some brilliant guy one day was like, OK, let's let's write her code this way, too, So you can model an object as a class and similarly, you could model an object also as instruct. And I want to explain, um, I want to exemplify two concepts here, one with the difference between these two things are and the notion of inheritance. So let's get rid of this for now. So let's say Let's stick with our example of the furniture and chair and let's let's create something that similar and analogy, if you will. So let's create a class called vehicle right and this class vehicle right below it. Let's create a class called Khar. Look, a car is a vehicle, right? So let's come up here and put a colon in front of the car and put vehicle there. So now I want to make sure you don't get confused that a car is. This isn't a instance like a variable of the type vehicle, but this is kind of similar. We talked about types. A car is a type of vehicle. So in essence, what we're saying is this car hierarchically is a subclass of a vehicle, So that's the proper terminology. This is a class. This is also a class, but this is this car classes, a subclass of vehicle. Now let's keep going. Let's do BMW now. Being W is a subclass of a car in a car, the subclass of a vehicle. Now we can. We can keep going as far down into this into this kind of endless rabbit hole as we want. But this is the idea of inheritance. Um, and every single thing is built with inheritance and swift and in most programming languages. So you can imagine, let's say, a button. So in Swift and Iowa's ah buttons, classes called you I. But in you can imagine, a button has a couple of similarities with other things, right? It's a user interface element. You can see it. Maybe the default size is a rectangle, so it inherits a bunch of other things. So now that we understand inheritance, let's talk about what actually goes in the class so similar to our example of how we have a chair and furniture. Things in the real world have properties, right? So a chair might have a size, a color of Finnish material and a church and do stuff right? Like you can sit on its hair. I'm so on and so forth. So my point of saying all that is in a class, we can put variables, and we could put functions. So a car Let's take a car instead of vehicle. A car can have a function, lets it Dr and a car can also have a color. Let's stick with string. For now, a car can also have a let's see model, whatever would go in there. So the notion is you can put variables properties. These are actually called variables, whether constant or variable or static in here, and we can all support functions in here. Now let's talk about another thing before we switch over toe Struck's and let's just get rid of these other classes. And let's now that we understand inheritance, let's ah, stick with this class So cool. So now this is gonna be yelling at us in just a second when we try to use this class for one for one key reason classes need initialize. Er's now what does that mean? So let's say we put let's we have this car and we put two constants in here. So we have let, um, make and it's gonna be string. We say let color, and that's also gonna be a string will come over here, and we're going to see that there's an error. And what this is complaining about is we don't have an initial Isar. So what's an initial Isar if you're familiar with a constructor and initialize er is the same thing? If you're not familiar, I'm glad you're here. So an initial Isar is a special type of function that helps you create a instance of this class. So let's say we want to create BMW. Let's say a variable B M w. And we want to create a car. Um, and this is how we would create an instance of this class. This is yelling at us because what we have said here is we have these two, these two, um, constants and they're both strings. But we haven't assigned them to anything, right, Like there's nothing in front of the string and it's yelling at us because it doesn't know what they should be. So we can do as we can provide something called in it. And in it is a special type of function that helps you initialize the class. Right now, what you can do in here is you can pass in similar to any function you can pass in parameters like so I could spell it correctly. So we're gonna have color, and we could have a make, and what we would have seen here is we're going to say self dark color equals color and self dot make equals make. So let me explain this because this actually gets a little a little annoying for some people, and this is complaining here because we're not using the initial Isar, so we'll fix us in a second. So what we're saying here is when you create this car, use this function right, even though we don't have the word funkier in in it is a special type of function and it requires, as we have said here, two parameters for a color and a make. Both of their types are strings, and in this actual function body in this initialize her body were saying self thought makers make and self dot color is color. I noticed the color distinction and noticed that we're using self dot. What we're saying is for this instance of this class that were creating assigned the incoming parameter value to the respective value here that signified by the self dot and that's really all that means. So when you say self that color equals color, the self that color is the color up here, and the thing on the right of the equal sign is the color that's coming in as a parameter. So if we want to see this being used, we can say something along the lines of our UMW equals car. When we open this, we get this auto complete and notice I want to color as a string we can say, Let's say something like blue and make we already called their variable being W. So lips. Now we have to use BMW, of course. But of course we can use something else as well. Um, yeah, that's kind of the nation nature of an initial Isar and Inn Super important because, as you can imagine, a class by default might not have a value assigned to the constants and variables that it has inside of it. And you might want that class to be configurable to a degree. So let's see, here we create another version of a car until we call this car, too. This could be like Honda, and this could be read. It basically gives us the flexibility to pass in whatever we want and create instances of this car with varying configurations. Now I keep saying the word instance. So what the heck does that mean? An instance is just a ah currents of that object that you stored into your variable, right? So the type of this is a car that's the class and it's a car. It's where initializing a car with these properties, and that's really all that a car, rather, a class is now a class versus destruct. So instruct is almost exactly the same thing. Is this? So let's actually let's literally call this instruct and notice that everything works as we expect. Nothing has changed. Nothing is broken. So what gives? What's the difference between instructing a class? The key difference. And I really want you to understand this as this is a guaranteed interview question. For Swift and I a less is a class is a reference type, and destruct is a value type. So what, I would I'll explain it. And then what I want to do is I wanna showcasing with an example. So a class, when you create an instance of a class like this BMW down here was BMW's. An instance of this class when we say Let's say we do have our car two equals B M W. What do you think happens with this? Would we be? Would we be saying this car to is pointing to be in W. Are we creating another instance of car? That's where the key difference comes up with classes and Struck's. If this was a class right now, we've changed it to instruct what it would happen. Is his car too Variable would actually point to BMW, so if anything changed on this BMW variable by proxy. This car to variable would change, too. So an example would be like we passed in this color. If we change the color, it's a yellow. And then we say, Okay, Car two. What's your color? It's gonna reference everything from the BMW variable. The difference of a struck is because this is a struck right now when we say car two equals BMW. What this has done under the hood is it's created a copy of being whatever being W is assigned to and assigned it to car, too. And they're totally different values. So if we say car, want car, If we said BMWs, color is now purple and this variables is new car. This car to its color is now magenta. We have purple and magenta totally as separate values on two different objects to different Struck's. So with a class, then it would. The nature of it would be car, too would be pointing to B. M W. And as B A W changes car to changes and instruct. Cartu is totally separate from BMW. However, when you say cartoon equals BMW, what it will do is it'll star. It'll create an instance of the car in here by whatever the values and BMW currently are, so it'll create a copy of it. So I really want you to remember this because it's really important. And I've interviewed many candidates that don't have a strong understanding of this. And I know this is a point where people's trouble and they feel toe realize what the importance of this is until they're kind of down the rabbit hole in a nap. And they're like, Shoot, I should have used this truck to your instead of a class. But they other than that they are very similar. Both classes and struck's can have variables, constant statics functions. They get created in a very similar fashion. So the name of the structure of the class and open close parentheses in the parentheses. If you have an initial Isar, you put the parameters. You do the self dot color self doubt, whatever your parameter name is, your property name equals that parameter. Very, very similar. Well, actually, literally identical. The key difference again. His reference type dresses value type, and I hope that's clear. And that is where I'm going to wrap this up for this lessons. We went over classes and Struck's. And we're going to start basically combining a lot of the pieces that we've been learning over the past few lessons and things like types and constants and variables and now classes and functions. And yeah, I will see there's Analects lesson. Please do leave a like follow form or content lead. Some comments, feedback suggestions. All of it is very much appreciated, and I'll see even the night classes. 6. Swift for Beginners Part 6 Loops: back to another lesson in swift for beginners. In today's video, we will be talking about loops, and this lesson would be fairly shorter as it's not as of it exciting topic, but nevertheless very important. So we're actually picking up exactly where we left off in the last lesson, talking about classes and Struck's. So let's get started by removing all this. So let's call this loops. So what the heck is a loop? So a loop as a kind of, uh, signified by the name of Loop is the task of going over a particular operation multiple times. You're kind of going in loops right and doing something over and over, and that what that actually means is, let's say you have a collection of numbers. Let's say you grouped a bunch of numbers together. Let's say 1234 and five, and what you want to do is for each of those numbers, you wanna multiply it by two. Right? So well, in theory, you could just pull out every single number and be like, OK, this times two and this times, too. But what happens if you have a collection of two million numbers? You're gonna have as many lines of code as as many as many numbers as you have. So for that we use loops and there's a couple different types of loops, and I want to go over two particular loops in this lesson that are very common. And the 1st 1 is four in and the other one is a while loop. So we're going to stick with their example of having a collection of multiple numbers, and we're gonna use an array for this. And if you don't know what Honore is, it's totally fine. We're gonna be comforting in the later lesson. But I want you to focus on loops for this lesson sake. So we're gonna create a collection, and we're gonna say we call it numbers and it's gonna have one comma, two commas, three comma, four. Uh, if once Let's leave it there. So we have this thing and it has these numbers in it. So now what we haven't used thus far is a print statement. So whenever you do a print, let's say we do a print of test down here in your this is called your consul. You'll get whatever you print out and super important, helpful to like, test out when you're actually developing an app. And also just for the sake of seeing in, like, a playground, learning as what we're doing here. So we're gonna be using this print statements. I just want to kind of introduce the concept of printing. So let's start off with a for in loop. So the four in loop does Is it says for any element in your loop, do something. So what that means is, in our numbers situation, for each of the things in here for that thing, for that iteration of the loop, do something and it'll go over every element in the collection. So what does it actually look like for thing in numbers? Do something. So what this is saying is, every time this loop runs, um, it'll the value at that position. So in this case, 012345 because collections air in new Marais tid from zero upwards working for us, get 12345 and six for the values and that value every time the loop runs, that value is basically the value that's inside thing. And where did I get this thing from in a four end. You can name this whatever you want. This is your variable. So the first time this loop roads, it's a one. The next one thing is a two than three. So on and so forth. So what we want to do in here is we want to multiply Whatever thing is just a number by two and printed out. So we're gonna say Let, um, results equal thing times to enter your printout results. So if you look down here, we basically now via these few lines of code were able toe loop over every single thing inside of this collection of numbers, multiply it by two for each of the numbers and printed out. So this loop ran a total of 12345 and six times. So that's a notion. Notion of a for in loop for thing in collection. So now what is a wild group? So while loop is a little different, but kind of similar at the same time. So we're gonna stick with his numbers collection up here a while. Loop has the notion of do something wild. This condition is met, right? So, uh, n blame its terms. Let's say while the sun is still allow it, be productive, you know, we like we should all be productive when the sun is out. But in theory, that doesn't always happen. But to prove my point, rather, let's let's use an example. So we have this thing of numbers here, right? Let's say, Let's create a variable A Let's call it X and let's say it equals zero. What we can do here is we can say while X is less than three print, we can say the thing. The current brother. Let's not even use his numbers for the while while loop example. But while X is less than three and notice X starts off at zero, let's print called and because right now we're not changing X. We're just gonna continuously via printing out called and we're gonna get stuck in this loop, right? The notion of an endless loop is super bad. As you can imagine, you kind of get stuck in the loop because it's just an infinite loop. So it notice this started, Ah, giving me a spinning wheel. For the sake of this, Lou just keeps running, which is slightly ridiculous. It should know better to stop. But let's Ah, let's close that x cud and we might have to reopen this year or so. Bear with me just a moment. So let's come down to exclude and let's open up our playground again. And let's quickly get rid of this before it starts running it so well, we're gonna do in a while. Loop is we want to be printing something while X is less than three. But we want to make sure that we increment x along the way. Right? So what I'm gonna do for the sake of doing this is we're gonna type out the body of the loop before we put the while. Otherwise it's gonna automatically start running. And to add a number to the variable X weaken do X plus equals one. And in front of here, we can say while X is less than three, we want to do something and we want to print out this thing called test. And if we come down here, we can see that we have test prints it out three times because the first time x zero cool little run the second time X is one and the third time access to and then the next time becomes three, and the while loop goes is three less than three. It's not somewhere just not gonna go in here now if we make this. If three is less than or equal to Rather is if X is less than or equal to three notice it prints at 1/4 test here, and I guess, to prove my point that it's also printing out unique kind of loops instead of printing a test, let's print out X, and we'll see how every time exchanges, and it prints out only that many instances that only loops over itself until this condition is met. So while whatever the statement here is true, do this block and you can imagine a while. Loop in a for in loop are very handy to generate over a large collection of data. So let's say you want to go over. Uh, let's say, like a list of messages. Anyone for each message wanted to leave a message instead of kind of going one by one. You might just do a for in loop for message in all messages and perform a delete function on each message, and that's kind of a practical example of how a loop can come in handy. And there's, of course, many other ways, and we'll definitely be encountering them along our way in our entire course. So that being said, I want to wrap up the lesson here. Ah, little bit shorter of a lesson today. I hope you enjoyed it. I hope you get an idea of what loops are, how handy they could be, what their practical application is. Please, do you leave a like if you learn something? If you come this video helpful, leave a comment. Let's start a discussion. If you need help with any of this, would be more than happy to help you guys out, and I will see you guys in the next life. 7. Swift for Beginners Part 7 Optionals & Unwrapping: I think it's welcome back to another lesson in Swift for beginners. In the last lesson, we talked about loops, and in today's lesson, we're gonna be talking about optionals and unwrapping optionals and a little bit of a preface. This is one of my personal favorite topics, and it also happens to hear topic that a lot of people struggle with including myself. So bear with me. We're gonna get through this together. So we're gonna pick up exactly where we left off with loops. So let's get rid of all this. Let's give this a title of optionals. So let's start off with what an optional actually is. So an optional is away in Swift, where you can signify that a variable may or may not contain a value, so an optional is actually a part of a type. So let's say we have a variable called name. And here is Joe right to write this. Moreover, boast we can write this. His name is gonna be a string, and it's gonna be Joe Now, what if we what if What if this app that we're writing, whatever the program we're making, is a app where the user can log out, right? Let's see when they log out, we want to empty out all the variables of the last logged in user. Let's see. Now we want to say name equals nothing. Which, in other words, is mill. Right. This is gonna yell at us because we've actually explicitly set up here. Hey, name should always be a string. So in theory, what you could do is this right name is a string vicious empty now, but this still accounts as a value. So what you actually wanna be doing is you want to say name is nil. And again, nail is just kind of nothing. Nil is it points to nothing. Name has no value, so but is yelling at us because it saying it expects a string and Neil is not a string. So what gives, man, we can't This can't work. So what we want to do is put a question mark in front of this string and this error will go away. And what this question mark means is this name may or may not be a string. And the reason that apple actually chose the question mark symbol to signify this is it's kind of like a question. So if you imagine, like, someone asking you, like, how are you today or like So let's say someone asks you a question. Um, and you kind of like you may you know the answer. You may not know the answer, like the notion of a question and a question mark is It may or may not fulfill what you're looking for, and that's why the question mark symbol was chosen. And a lot of people get irked of why this was chosen. They find it confusing, but that's why it was chosen for some context. But now we can save his name is no. So now this adds a little bit of, ah, complexity because because Swift is a strongly typed language, the type of this now is not string. The type of name is that string optional, and it's super important that you understand that a type that is also optional is completely different from just being that type. So if we have an int and then we have an int optional, they are two completely different things. So what I want to signify here is let's see we have let's say we have a variable and we call it X. And let's say it's int optional and it's three and let's take this and we say This is easy and this is 12 right? We have two variables and they're both into optional, but they both have a value of it. End in theory. Down here, we can say there mil because we specified the question mark and say that it could be int or it could be nothing because it's optional. This is cool. But now what if we wanted to do well? It's actually make this. Why what we wanted to do results equals X plus. Why Now it will yell it us about this. And now why will it yell at us about this? Because technically, X and Y are both numbers. We should be able to add them, but that's where people get tripped up. X and Y are not. It's X and y are into optionals and a type that is considered something optional. Think of it as a box. The actual type. Anything optional is a box. The value is inside the box. So here what we're basically saying is, we're saying, Hey, add box X and Box Y Swift doesn't know that you're actually referencing the value inside the box, which in this case is three and in this case is 12. So this brings us to the notion of unwrapping an optional. And that's why the the terminology unwrapping was used. And that's why people use it. And that's why it's officially supported by Apple in their documentation. The notion of unwrapping is kind of looking in the box to see if there's a value before you try to use it, because we've said that this might be nil, in other words, might not have a value. We don't want to try to add two numbers together if one of the numbers is non existent. So how do you unwrapping optional? What you basically do is you say, if let value equals X. So that is, where did this value come from? Where did this if come from, where those let come from, etcetera, etcetera. So let's dive a little deeper into this. So what we're saying here is we're saying, if conditionally, if there's value equals X, In other words, if something is inside of X, we can use it in here now Where did this term value come from? This term value. We kind of just made up. You can call this whatever you want. It does not have to be the term value. Are anything remotely related whatsoever? It's us creating a constant, thus using let of the value inside of X. So if we actually put a print in here, I'll say has value. And if we come down here, we'll see in just a second that it does come into this block because it does have a value. But if we take this and if we say this is now nil, notice that this print should go away, which it does, and it goes away because we're saying if there is something in X, come into here, otherwise you can't come into here and right now it's nail so we don't come into here. And what this is complaining about over here is we've created his let value, but we're not using it below, so it's kind of like him, and you created this constant, but you're not using it. Why did you create it? So let's actually make this three and let's delete this print and in here let's create another. If let me just say value to and it's why and then we can say we can say result is value plus value to and then we could print out the results and the reason again that this can work now. Using value and value to these two constants is value in value to actually point to the thing inside of that kind of black box that is known as an optional. So you don't want to add the actual boxes together and again, the way that the method of explaining as a box is something that I like to use to explain to those that I teach. And that's also kind of how I learned. And I think it's helpful to visualize it. You that you can't add two boxes together. You want to take the numbers in the boxes and add them if the numbers exist. Now that's the notion of an optional and how'd unwrapping optional and the reason they call it on wrapping is is kind of like a box, so now I know what I've showed. This example with sincere anything can be optional. So let's say we have let's say we have a variable here and it's price and it's gonna be a double optional notice that before, if you recall from prior lessons, if we didn't have this and we didn't assign a value, it yells at us and that will ya let us here in just a second. Well, if we put this inside of a classical yell at us and the reason it will yell at us A. We don't have an initialize er in this case, but, um practically let's try it this way. Let's see if this wants to cooperate today, so it's not wanting to throw the error right now. But the reason that syntactic Lee this is not correct in an actual application is because this is a constant and we said it's a string, but we didn't assign a value, but because it's a constant, we can't come down here later and be like this equals one. That to three in an actual application here is letting us do it. I think for the sake of because this environments a playground and it lets you play with language. But what we could do here is we can say it is a double optional So by default, if we do a print of price and then now here we do. Price equals this and we do a print of price. We'll see that this price initially. Well, let's actually do it this way. Let's see. Well, it's not gonna print properly, but let's do Let's make this a variable and let's do this is nil. And let's do price is now one that 23 The reason it was yelling before is, but we had it as a let. And as you'll recall from an earlier video, we cannot assign a value. We cannot change the value of a constant, which is why I was yelling. But let's dio print price. And before we change it, let's do a print of price and you'll see initially prices nil. And then the price is this optional with our value inside of it. So price isn't actually the the number we assigned here prices optional with his number inside, and I'm gonna stress over the last time not to beat the dead horrors. But if we is a type of price and we come over here, if you think the price is the type of price will be double. That's incorrect. The prices optional double or double optional. Most people read it optional double, but let's come over here and we'll see that it's actually an optional double, right. The double is inside the optional because that may or may not be no. And that's, I think, a good spot for me to kind of end this and this lesson without being more redundant. But optionals are very important, and they're used literally all over the place due to the nature of a lot of APS. So you can imagine, um, if you open the Facebook app and you try to get the APP tries to load your feed and some ever occurs and nothing comes back from the request that was made to get your feet your posts, the type could be a class called feed, but the variable would be feed optional, right, because it might have a feed, but it might not, because something might go wrong. So Optionality is a very good way to represent errors and kind of give you. Give yourself some flexibility to move around in, ah, your code and change some stuff up and not always worry about having a value assigned to a particular variable or constant. So, yeah, with that being said I leader, like if you found this video helpful, please, of a comment or distorted discussion if you need help, clarity more than happy to help, and I'll see you guys in the next lesson. 8. Swift for Beginners Part 8 If Else Conditionals: welcome back to another lesson in Swift for beginners. In the last lesson, we talked about optionals and unwrapping. So if you didn't get a chance to watch that one, I encourage you to go back and do that. In today's lesson, we're gonna be picking up exactly where we left off and we're gonna be talking about condition ALS. So let's start by getting rid of all of this and giving a title of condition ALS and getting right into it. So let me just preface by saying this video will be a little shorter than the last lessons . As, uh, it's a fairly simpler topic. So conditional, kind of liken implies is a way to represent conditions in your code. And you may or may not have seen this more likely that you have if you have seen any other code elsewhere. This concert of conditional is is very common amongst other programming languages, but it is an if else condition. So let's see, we have a variable Excellency, it's too. Let's say we want to check If, um, the FX is bigger than 10. We can say if X is greater than 10 do something we can also say, if it's not, in other words, if this doesn't hold up else, do something here. Now we can build on this and you can actually build pretty big like pyramids of conditions that we can say else if X is greater than 20 do something else. Do something. So it's interesting about this is this condition will start at the top here and it will only go into one of these blocks, right? And when I call a block, is this this little space inside the currently where we can do stuff? Let's say X Woz 12. The problem with writing the conditional this way is, is ex bigger than 12. Rather is 12 bigger than 10. It is. So do you. What's in here is 12 bigger than 20. It's not someone. Come here. But what happens if we make this? 42 is 42 bigger than 10. It is. So we're gonna come in here and stop, but it's 42 bigger than 20. Well, it also is, but we never got the chance to come here. So condition statements are a way to test conditions against variables, but we want to make sure we write them in a hierarchical way where they kind of gracefully fall to the next thing you're checking. So that a better way to right this would be another correct way to right. This would be would be would be to check 21st then 10. Otherwise, you know, do something else. So what? This would prove what this would rather make it safe to do. Would be is, let's say, with you 19 we can say, OK, it's 19 bigger than 20. Well, it's not. So let's go down one more. But it is bigger than 10 to come into here. So if you recall from our last last lesson Teoh unwrap optional. We did something as follows. So let's say we have the exit is an inter optional on equals three and unwrap it. We did it. Flat value equals X. We could use value in here, right? Notice we did a If let so. This is also a conditional wrapped with creating a constant. And what this conditional is checking is if there is a value in this optional object. X put that value in this constant we created called value. Let's actually call this riel number for sake of clarity. What we're going to say is put the actual value that is inside of X in Israel number and come into this block. If this was nil, for whatever reason, it's going to try to do this condition and say, Hey, if there is an actual value, put it into this real number constant thing and come into this block But of course this is nil, so it'll never come into here. So that's the notion of conditions. You could have a variety of condition operators and operator is something like greater than less than in this case. This is optional checking. That's a conditional operator. It could be if something is divisible by something else. So in an earlier course we did. Whether an earlier lesson, we did something along the lines of we have this variable X. We want to say if X percent too equals zero does the condition. So what we're doing here is we're seeing if two can evenly body divided into X, in other words, is a remainder of zero come into here. So now this was 11 to could only be divided into 11 5 times, but there will be a remainder of one. Thus the remainder will not equal zero. It will not come into here. So the point is like this little spiel is there are a couple different operators, um, greater than less than, uh and you can actually connect multiple conditions together with either a comma or an or statement or an and statement. And that's really powerful and important because let's say you want to check to conditions on the same line and you want to come into this block if either of those conditions hold up . So it's a we have our X, and it's one and we have var. Why? And it's 12. And we want to say if X is greater than 10 or why is less than 200 come into this block? These two lines that are often called pipe operator symbols signify that if either this or this holds up in terms of the actual condition, you're allowed to come in here now. Similarly other inversely you can use end so here you're checking. If this and this is correct, you can come into here. It's oftentimes you'll see some conditions that are, you know, connected in this manner as to rather opposed to doing, like, one condition appear and then taking this in doing this and doing this, it's kind of cleaner and nicer to put it onto one line. And it helps to readability and to understand and to read more concise code. So that about does it for conditional. I hope you found this lesson helpful. And I really would encourage you to leave a comment to give me feedback so I can improve and ask any question that you may have leave a like in this video, please. Do you follow in my other videos? It helps out a lot. Um, yeah. I'll see you guys in the next lesson. 9. Swift for Beginners Part 9 Guard Statements: welcome back to another lesson in Swift for beginners. In today's lesson, we will be covering the Guard statement. So once again we're back in our playground. We've gotten rid of everything from the last lesson that we have our title of Peter. So let's jump right into things. So a guards statement eyes fairly common in Mawr more so newer languages and what people have to call functional languages. And it's very similar to an if statement that we saw an hour if else conditions video. So let's just start off by writing a function. Um, and let's call it string equals. Let's call it a number larger than five. It's a number larger planned. Five. Let's go with this. It will take a parameter of a number. It will be an end. This function will return a pool. Also say returns true. So a guards statement. So before we even talk about the guard in theory, what you would do in here is you would say, if number is less than five, let's return. False right does make sense. Um, and this is complaining because hopefully that's good. Yeah, So, basically, if the number is less than five we're gonna say false. This number is not larger than five. Otherwise, it won't come into here and we'll go to Troop. So how would we write? This is a guards statement. So all we would do extremes this to guard, Um and this would be an else. So is very similar to if else. But we're basically saying in the statement is very similar issues ward it a little differently. So in layman's terms, what you're saying is regarding you want to make sure that number is greater than five. Right? Um so it's the inverse. So if the number is greater than five, you can continue down here. You're guarding against this case. But if the number is not greater than five, go into this else, right? So in other words, if the number is that is less than five, we're gonna come into this else and we're gonna return false. And that's gonna be the end of our function because, you know, we returned already here. We can't go further down. The code execution in our function has stopped so you can see the similarity between the two statements. But you can also see that regard if you throw it onto one line like this is a little cleaner in terms of visualizing it and readability sake. But that's all the guard really is. It is a little fancier way to do it if else condition the other big benefit to a guard similar to an if condition is you can comma separate in here. Um, multiple things that you're guarding is you can say if number is greater than five and number is greater than four. Which, realistically doesn't make sense to do this in an actual app, because if it was better than five, it would be greater than four as well. For demonstration purposes, you can chain on multiple things you wanna guarded. So, for example, let's say you are building a log in form, and the person needs a type and a user name and password in an IOS app. When they press sign in, you want to make sure that there is text in both of those fields before you try to sign the person, and so you might do something like guard. Make sure this field has text and guard. Make sure the passenger field as text. If they don't, you could maybe throw like a pop up and show the user an error message and say, Hey, you need to enter in something. So we consign you in. That's what a guards statement essentially is. Now. What I want to show you is the other very common thing that a guards statement is used for . And that's for unwrapping optionals. So if you're not familiar with optionals, are unwrapping them, encourage you to go back to an older lesson and take a look at that as it's super important . But what we're going to do in this case is we're gonna say let um, text, which is a string optional equal milk and done here. We're going to say this of ours. We could reassign it. We're gonna say text A to this point is gonna equal Hello, world. We actually don't even need this because this is nil by default that we can give it a dysfunction. But basically we've created a variable. It's text Thebe is string optional and we're setting it to hello world down here. So now if we want to use this tax, actually get access to it in a prior video. We've seen that we need to unwrap it. And we would do that by doing something like if let value equals text. Then if we print value, will see down here that we get our text because something optional is again kind of like a box. And you need to check that there is something inside of said box so you can get at the actual value so you can as you can kind of imagine, do this through a guards statement as well. So we're gonna do guard, let value equals text else return. Um, and then down here, we can say print value, and this might yell at us because we're doing a return outside of a function which it is. So let's put this in a function. I want to put this right here. Um, hopefully that should saw that which it does. And then let's get rid of this print so we can call this function and make sure that's working. So it's call print value. And at this point, we should get Hello World on here, which we do. So now Imagine if we didn't set the text here. So if we go and comment out this line we won't get a print here because it's gonna go into this function is going to say, Hey, I'm gonna guard and make sure there's a value inside of this text and if there is, we can keep going. We're really good to go. But if there's not just return out of this function and don't continue on so it's a little more readable in my personal opinion you're guarding something is there is correct and your criterias is fulfilled. Um, and it's less of her boasting a if statement. So using guards to unwrap optionals and using guards as a simpler way to replace if else conditions is wildly popular and swift. And it's something that I strongly suggest that you be comfortable with. You might not have seen an older languages, older programming languages if you're if you have any background in older programming languages. But that's essentially what a guard is, and I think that's where I will leave it for this lesson. I hope I was able to explain guard statements while to you. If you like this video, please do leave a like follow subscribe share. It helps allow quite a bit. Don't hesitate to leave comments in terms of clarity. You're asking for help or questions more than happy to help. And I will see you guys in the next lesson. Thanks for watching. 10. Swift for Beginners Part 10 Enums & Switch Statements: thighs. Welcome back to another lesson in Swift for beginners. We covered guard statements in the last lesson, and in today's lesson, we will be covering Denham's and switch statements. So where we will be picking up exactly where we left off with guards statements right here . So let's start by getting rid of all of this. And let's ah, change our title appear to show that we are working with Denham's and switch statements. So we're gonna start with Denham's, and then we will transition into switch statements, as I think they kind of go hand in hand to a degree. So Enaam is a type of data structure slash. Way to represent is not really a data structure. Exactly what its way to represent multiple cases. So in, um, let's start by creating one so we can actually see it. So you would use a cured in, um, and let's call this, um, let's call this in. Um, states right and in the genome can have multiple cases. So let's say this enough represented states of a person registering for a particular service, right? We could have a case that says, um, in progress, we can have a case that says, um a boarded. We could have accused us as complete. Maybe we can have a case that says will start So, you know, basically is a way to represent multiple states on and you do that through cases. And this could be super helpful for the sake of kind of adding guardrails around what your program in your code actually does. So someone might say Lee, Well, wait a second. Why can't I put this in a string? Why can't I do something like var? Um, current stayed and I could do it complete. The answer to that question is, in theory, you could do this. However, this aligns with some guard rails in terms of ensuring the any developer engineer looking at this knows that this particular usage of the state thing can be one of these four, right? And it also ensures, like you don't make any typos insurers. I casing is consistent throughout your uses of it, and it just makes things a little Tidier. So for that reason, cases air pretty, pretty important and pretty heavily used now before we go to switch is I want to demonstrate that, and Swift you can shorthand, a lot of things, so in a lot of languages in them exists in terms exist overall. But in Switz and Swift, you can shorthand things a lot. So we can actually just comma separate these and really short in what we actually need to type. And this is perfectly correct. You're just assigning. You're just letting me even know that these are the cases available. What you can also say is, um, the Denham inherits something so Denham's, similar to classes and struck's, can inherit particular, like other classes. So we can say this inherits string, and what this allows us to do is we get a sign of value to each of these. Let's say, like we want a specific, like string value. We can do this here and to actually access it, we can access the string value off of the case. Now I haven't Should you have access it toe? Let's, um, let's get rid of this stuff for a second and let's say we want to represent um, let's do current will be states dot Let's do in progress. So the way you represent this in code is first you put the actual in um And then with the dot you basically add the chain notation of what particular case you want A really cool thing about Swift is because there is this notion of type inference ing and the language is smart enough to understand what the type of something should be. You can actually really shorthand this again. So let's say we have a variable called current and it is of type states optional. Right. So this this current is gonna be one of these cases, but also optionals. There could be no, let's say, sticking with their example of starting an application, but to the person starts and this becomes in progress, right, This current variable, but you can do here is do current that in progress. And the reason you don't need to do states before this dot in progress is because we've already told Swift the type of this is gonna be some case off of this states in, um so we can shorthand the heck out of this. So you might sometimes see that people do this for colors. If you would have signed the color of let's say, like a but in if you're making like if you're making, like IOS up and let's not worry about this import. You like it for this purpose. But let's say we have color and this is gonna be a US color. We could say this is like red and let's make this color So it knows that we're talking about and this is perfectly correct. Now, of course, you can also do this and this is there's nothing wrong with this, but it's just a little longer and more verbose. And people generally like to write the bare minimum amount of code for a stake of being lazy and be sake of reading less code. So that's an enema for you. And, let's say, sticking back with our state example. Um, let's say we added the inheritance of a string here. And let's say this equals one. We can say this dot raw value and let's see if I need to do anything else. Well, it appears I can't do it in, ah, this particular environment, but, um, to get the actual string out, you need to do dot raw value, and it's giving me an error at the moment because I I'll have to set up a few other things around it. But that's a little tidbit that I didn't want to show you guys, if you ever come across it, not the most important thing. Understanding Denham's is the fundamental here. But I just want you to know that you can indeed access whatever year, whatever stringer assigning this case, too, if you wanted to be different for whatever reason. So let's get rid of this and let's start talking about switch statements. Let's get rid of that now. Let's create a function check, states right and a switch statement is basically similar to a K A and E nomine cases is a statement that switches over a variety of cases and a variety with, like, a variety of criteria. So in Swift, a switch statement has to be exhaustive. Now, what that means is, before we write one out is when you're switching over, let's say like numbers and you know the numbers can be 1 to 10. You need to say switch number, case one case to case three all the way to case 10 you have tow, basically provide the language instructions of what to do with each unique case. So what, we're gonna do in our little example Here, we're gonna have current is gonna be states not in progress. And in here, we're going to switch current and the case can be, um, in progress. It's not wanting to cooperate today. Well, it doesn't know the current is Ah, States. I was just copy and paste it for the sake of Ah, this not wanting to cooperate today. But we can basically dio in progress, and we can basically do what else did we have a peer a boarded And let's put this in here. Actually, there we go now making progress. So, um, you need to Curly's. We need to put what we're switching and the various cases. So we want to switch. Um, the in progress, the aborted what else was in here completed? You also want And what's the last one? We'll start and you're gonna put a colon after each of these. Now, what this is basically saying is four each of the states that it potentially could be. What we want to do is some bit of code, right, And this is exhaustive because there is nothing else that this can be. And this is actually complaining because A We have an extra one here and we need to put a brake here for each of these. I'll explain what a break is in two seconds. I apologise, my computer fan, but basically what we need to be doing here is in each of these statements, each of these potential cases, this state thing could be this current. In this case, it's in progress. We wanna check. Hey, is it in progress? Is it aborted? It has me one of these, right? And we want to execute some code, so it's if it's in progress. We want to show, like show, um, info form. Or we want to show, like a welcome get started page or like anything. But the point is, it's an exhaustive statement, and you're switching between a variety of cases, and it only goes in tow 11 or the other. Um, this could be modeled similarly as a if else condition. But you can imagine you can have. You'll have for Let's say, you have any numbers, like five cases you have multiple if else's and it becomes a little hard to read and not the prettiest of looking code. So people will use a switch statement and this break basically signifies Once you're done, execute, let's say so. Let's This current is in progress, which it is once you're done executing whatever code we put in here. So let's put like Prince here. If we come down here, we'll see what we need to call this function. Let's come down here and call this function, and we should see you here printed out which we do because current truly is in progress. We assigned it up here once. You're done doing whatever bit of code is in the particular case that you got got into break just held. OK, finish up. So you get you can think of this a synonymous to return. You just want to tell the switch statement. Hey, I'm done. You get to go. Don't bother even looking at the code down here. Let's say you got down here and this case is true. Don't bother coming down here. You're good to go. You're don finish up. So that's the essence of a switch statement and the reason which hopefully is a little clearer now. The reason I wanted a pair switch statements up with case cases and Denham's is because it kind of go hand in hand so you can switch cases in. And, you know, it's pretty common you can also switch. Of course, things like strings are numbers. Or basically, you can switch anything, anything that you can compare in the language. So if a number is 12345 or if a string is, um A B C D e, like whatever you want to do, you can put it in a switch statement. But the important thing is it needs to be exhaustive. And if you don't make it exhaustive and you don't exhaust all of the possible conditions, Swift will yell at you, and it will actually give you a little. But in on this is fixed. Me and it will be like, Do you want me to add in all of the various cases and if you hit it, it does it for you, but it's important to understand the concept. So what that being said? Minor review. So we wouldn't every NMS and switches Denham's represent one or more cases. Switches could switch over multiple cases, whether it's an enema or something else, and that's about it for today's lesson will be ending it here and please you leave a like comment. Ask for help. Reach out, follows, subscribe. Share the video. Let me know what I could improve. Let me know if there's anything I need to do. Better to explain these things to you, and I will see you guys in the next theme. 11. Swift for Beginners Part 11 Protocols: things. Welcome back to another lesson in Swift for beginners. And today's lesson. We will be covering protocols, and with that being said, Let's get right into it. So a protocol is a pretty simple concept. I don't want you to be intimidated by the fact that it's called a protocol. It sounds a lot fancier than it actually is. So a protocol is commonly referred to as one of two things either a common contract or a blueprint, and we're gonna kind of exemplify what a protocol is through an example. So what I want you to do is create two classes. The 1st 1 will be a car class in right below it. Let's do a class called POW, which is a car from our, um what we learned about inheritance. And now we're gonna create a protocol So particle hold functions and variables, but it only holds their definition, not their actual body. So functions body is everything in front of the in the curly braces and a variable in terms of in a protocol. The variable is just the name of it, the type, and if it's a get or a set, you don't actually assign the value of it. So let's say you had a variable color You in the protocol, you would not say color is blue, red or whatever. You would just say I have a variable that is called color. You can get it and set it kind of. Similarly, what once you have a protocol, a class can conform to it. And the term conform is kind of just a fancy way of saying that the class promises that everything that the protocol declares inside of it being a function or a variable. Um, that class will have all of those things. So let's actually do one so we can see what the heck all that means. So the creator protocol, you literally type in protocol. You give the protocol and name generally with a uppercase letter to start with. So let's call this, um, car proto. And in here we can define variables and functions. So let's start off of the variable called color. And instead of giving a value to it rather before we do that, we want to give a type. So let's call. Let's make it a string, and what we're going to say is this color whoever conforms to this. In other words, whatever class promises toe have this, it should also have the ability to get it and set it. So get inside is a very common computer science term and nomenclature vocabulary. And what it's basically saying is I need a way where I can get the value of whatever is in color, and I need a wayto set more than once. What the value of color is this variable color? Similarly, we need to find a function here called Yeah, let's go Drive. It's really called Dr and Notice we don't want include these the curly braces. We just declare the function. So this is a funk called Drive. It has no parameters. That has no results. Um, let's do another function called is all Well, Dr. And this could return a bull. So this is kind of the nature of a protocol. It defines functions and variables that another class is gonna promise tohave an included implement. So the class itself would have these functions and have actual implementation of them in the curly races. So, for familiar with another programming language, let's say Java. This is very, very similar to an interface. So if you're even if you're not familiar, if you ever reading about interfaces on like a stack overflow example for sake of this video or anywhere all mine this is very similar to an interface. So now that we have this protocol defined up here, it's kind of a contract we've laid out. How do we tell this BMW class to conform to it? The way we do that is after this car that we're inheriting from, we're gonna comma, separate and put in the protocol that it should conform to and a class and conform to more than one protocol. Rather, it could be zero or more and notice We're gonna get an error here because that particle basically has two functions in a variable. But our class at the moment doesn't have that. So if we actually go here and hit this little ah warning icon, it'll ask if we want to have X code. This environment automatically put this stubs in for the stuff we need. And we're gonna hit this button to do that. If we go back here, you'll notice that included the variable and the two functions. And instead of having just a function definition here, which is that's just a signature without the body. Here we have the actual body, and we have It's like a placeholder toe actually put in some code, so we're just gonna do just gonna do return? True. So this doesn't give us an error. And Well, this is also gonna complain about is we don't have a initialize er So we're gonna again hit the thing that hit fix and adds a rather I guess it didn't add a function, but what it actually did is it wanted us to have a value for this for this variable. Um, so if we don't have a value initially, we need to have a its function and we can use this to initialize are very variable, as we've done it in a prior lesson. And I believe that should be sufficient for the IRS to go away yet which it is. So the notion of a protocol kind of sets out guard rails, if you will, for different types of classes, right? So let's say you have a car class and there's a couple of different classes that are of type car. Let's say BMW, Mercedes, Lexus Honda. All of those car classes need the ability to drive. Maybe you want the ability to get the color on it, set the color on the car. So what you can do is you can create a protocol. That's, um let's say we would name in this case is car protoblood a more appropriate and would be car configurable or car configuration? And each of those classes would conform a k A. Promise that I have all the stuff that defined in this particle. Um, and I wanted to give you a real world example. Um, let's say you're charging in an IOS app. I'm sure you've seen people offer in APP purchases and let's say you're charging a user 99 cents toe unlocked the pro version of your app. So there is a protocol for the transaction object to observe when new transactions are put into three Q, which, in other words, kind of just means you put that 99 cent transaction into the shopping cart. And there is a particle which listens for any changes on that shopping cart type object, and every time there is a change that calls a particular function, So the class where you actually have the user go and hit the buy now button. To put that thing in the car and present a confirmation screen, you need the function that says did complete purchase or did cancel purchase, etcetera, etcetera. And a lot of the functionality is written by Apple in a protocol, because as an engineer, as a developer, creating your APP, you don't want to have to write that functionality out in every single place in your app where you have some payment option, right? You want to be able to conform to that protocol and implement those functions. So on that note, I wanted to cover one more thing regarding protocols before we quickly kind of summarizes and finish up. And that is, um, the naming style of delegates and data sources. So you may, you may have seen. If you haven't seen it already, you definitely will see that sometimes you'll see a protocol name be something like something something delegate or something, something data source and that something. Something is usually a class. So let's say this would be car delegate or it could be card data source so delicate in data source. But I want you to understand, and a lot of times people confuse this on. Of course, we have to change this down here cause this doesn't exist anymore. Delegate and data source is a naming convention that Apple came up with around 10 years ago , and anything that's a delegate in data stores 99% of the time is just a protocol. It's just literally just a naming convention, so don't get confused that it's something else. But the reason those naming conventions air heavily used and were chosen are for the following reason. A protocol that has a term delegate at the end of it as a Suffolk's generally houses functionality a k functions that deal with user interaction. So let's say you have about it on the screen and you wanna have a protocol for what happens when the user taps the button. You might have a button delegate and similarly, what happens on the person when they use their taps that hold on the button. That would also go in the delegate, so a delegate is usually tied to functionality in your code that needs to do rather that has to do, um, user interaction stuff whereas a data source says the name kind of implies deals with data . So let's say a but in needs, Uh, let's say about it, needs to know its color. So Weaken said it's background color. It can get that from a variable in the data source protocol or indeed, just needs to know the font color or font size. So, in other words, the data sources MAWR data related functionality and a delegate is generally mawr interaction, user interaction based functionality. Now, with that being said to take it with a grain of salt because it's just a naming convention , I have personally seen, um, those two things delegates and data sources used in different ways. But that is the popular naming convention that Apple has set forth. And if you go read through some of the code and some large company libraries like Google, for example, they do a very good job of following that pattern and staying consistent with it. And it really helps everyone offended the day because everyone kind of is on the same page . And no, what delegate data source are generally supposed to be doing. So it really makes developing understanding other people's code a lot more easy, but we're gonna be seeing delegates and data sources on other videos particularly probably another course in which we do more Iowa specific development. And we apply this all the swift knowledge to making an iPhone app. So with that being said, I'm gonna end the lesson here. So again, to recap we covered what a protocol is the fact that it can have functions. It can have a variable, but again, it should not assign its value in here. You need to tell it the type does get set specifies that you can. The class that will conform must be able to get an set. The notion of conforming is a class promising to include everything that the protocol has that it's conforming to and down here. We have all of that stuff with these functions and this variable. And this is how we would comma, separate and say we conform to this particle. And with that being said, I'm gonna end the lesson right here. Thank you so much for watching leave a like, follow subscribe comment on. I will see you in the night 12. Swift for Beginners Part 12 Strong vs: Theo. Guys, welcome back to another lesson in Swift for beginners. In the last lesson, we covered protocols. And in today's lesson, we're gonna be picking up right where we left off, and we will be covering strong versus weak in the context of memory management. So let's right, let's jump right into things. So let's start by removing all of this stuff that we have going on in the last lesson. And let's change this too strong versus weak and get ready to it. So before we actually go into strong versus weak, I want to preface this with a little bit of an intro about memory and memory management. Seeing as this is an intro course for beginners, I think that would be appropriate. So yeah, so when you create a variable in any program, whether it's in swift or any other language, that variable needs to be stored somewhere for the duration that your program is running and that variable gets stored in memory. So when you store something in memory, we need to specify programmatically when the the code and the program is aloud and okay to get rid of that thing in memory and that is kind of the nature of memory management. So if we create this variable called STR and it's equal to some string, we want the program to know when to get rid of this. Str and memory. In other words, when we are done using it, it would be ideal if the language new like Okay, we're not using it at this point, so you get hence get rid of it instead of having to tell the program in Swift. Hey, I'm done with us. Get rid of it. So back in the day many years ago, the latter explanation that I gave where you have to tell the programming language yourself that you have to get rid of something in memory, you have to do that yourself. It was very manual. Now it is. There is his concepts, particularly in Swift and Iowa's development, called strong and weak variables. So, first and foremost, how do we create a strong or weak variable? So we basically just prefix the variable with either weak or strong notice. It doesn't highlight weaken, prefix it with weak, and if we don't have weak, the variable is strong by default. Now what the heck does that actually mean? So I'm gonna explain this with a kind of an anecdote. As I have learned firsthand, people understand the best this way. Strong and weak are two different ways to tell a swift what type of usage that variable will have and, as a result, when it's safe to get rid of it in memory. So imagine there's a kid holding a balloon. Let's say those air two different variables. There's the child's. And then there is the balloon. There's a tiles, class and balloon class the and to specify. Actually, the balloon is a helium balloon, so it's up in the air. The balloon would be a week object, and the child will be a strong object, and the reason behind that is if the child disappears. The balloon also disappeared because the child is holding on to its and the balloon will fly away, which is why it's important. That's a helium balloon, Um, but vice versa. If the balloon disappears, the child is still there. So the thing holding onto the balloon is the child. So if we translate that back to our example in code a week, variable will be deleted for memory will get what Swift will tell it to go away if there is no other strong variable pointing to it. So let's give another example. So imagine you're walking your dog and let's see your dog really wants to run away. Your dog is the week object, and you, the person holding the leash is the strong object. And I keep saying Object. It is an instance of an object, so it's a variable. And if you go away, the dollar's gonna run away. Both things were gone. If the dog goes away, you're still you're still strongly created, so you're still there. So by default, things are strong in swift variables. But if if we want to make something weak, what we do is we prefix this with word week and let's see an actual example of us. Let's say we have a class. Why is is yelling at us? Let's see. Did they change it to put it up here? No, that's not correct. Okay, so let's actually see an example of this. So if we have a class called Child similar to our story and we have a variable in here called Balloon, let's say it's a string we can make this week. Because if we create an instance of this Childs, this has created strongly. However, if this was also created strongly, both things are strong. For the sake of this video understanding strong versus weak. What we wanna understand here is once this thing goes away, we're no longer using it. Everything that is weak that's connected to this as a proxy will also go away very similar to the blue and child example. So why do we want to do this? Why don't even care about this? You can imagine on a mobile device like an iPhone or an android device or any any device in general, but especially on a mobile device, there are limited resources and there is not an unlimited amount of memory. So you really want to be conscious of how much memory you're using, and this is a fundamental that I would highly and strongly encourage to understand and learn from the beginning. As this is extremely important in, uh, interviews, it's a pretty pretty sure interview question. And at a more senior level at let's say, like Google or Microsoft or Apple, Um, this is helping something that they focus on quite a bit in terms of performance. Um, so knowing how much memory you're using and utilizing the least amount possible to get your thing toe work is a very good skill tohave. That's kind of why we care. Eventually, if we just keep using an unlimited amount of memory, we're gonna run out of memory and our program will crash. So that's kind of where I want to end those video. Before I do that, maybe I should just explicitly kind of say it on and assume not rather not assume. But any variable can be created a weak or strong eso. It could be an object. It could be a primitive. It could be a number of string. It could basically be anything. So what? That being said, I will end the video right here. I hope you guys, I hope you guys learned the difference between weak and strong. If I could clarify or answer any questions, don't hesitate to put them in the comments. Please do Leave it like subscribe, follow. Um, share the course and I will see you in the next video 13. Swift for Beginners Part 13 Closures: back to another lesson in swift for beginners. In the last lesson, we discussed strong versus weak in terms of memory management, and today we're gonna be discussing another one of my favorite topics, that being closures. So let's jump right into things. So let's actually just title this as closures and let's get started with this cool. So a closure is something that's kind of tough for people to wrap their heads around. So I'm gonna do my best to explain this on and please leave a comment if I could clarify in any possible way closures air very commonly used in Swift so again, very important topic. So a closure is simply a function that can be passed around in computer science. This is also called a Lambda. So let's actually put this up here. So what does that mean? So let's start off by creating a function. Um, and let's say is greater than three. It takes in a number, let's say, as an integer and return Zabul, and let's just say we can say if number is greater than three. Return true, otherwise return false. So this is a pretty, pretty simple function. We've seen a function similar to this in the prior lessons, we have the name of the function in the parameter that goes in what it'll return. We do a basic greater than check in here, and we have a return. False. If, um if it's ah, not in this in this condition, if it's not greater than three, if it is greater than three, we return true. So well, we can do. For a closer sake, let's create a variable and call it mine function. And if we take a look at the signature of this function, this function takes in an int. It returns of bull. So this could actually be represented as a type of taking in an end and returning a bull international so it doesn't complain. So let's see. So this actually needs to be in parentheses as those air the parameters. So let's take a look at what this actually is. Doing This is is defining this variable name and this is the type. But wait a second. How can a function be a type? So a function is, if you think about it at a higher level, is just a named set of code or instructions that takes in the parameter and return something and optionally it might not taken anything right. So why can't we define this as a as something in a variable? We can and that is what closure is. So let's say this equals number in and let's take this code and put this in here. So this function here is completely equivalent to the value of this variable. So well we can do here is say this and pass in. Let's say four. Well, we could also do. Similarly, here is called This Function and passing four. So the difference is while one is a fully defined function, this one is a let's see Wise is complaining. Well, this were saying this was an optional so let's get rid of this, which is why it's complaining, because we have to unwrap it first. But But this one up here, for sake of difference between this, too, is the function assigns to a variable, and the power of this is weakened because we can pass variables around in tow. Other functions. You can start to think about how complex you can create function chains where you can have one function you call and a parameter you passing could be another function, but represented as a variable, a k a closure. So let's explain this syntax a little bit. You're saying the type is a function and the function is this whole thing in forensics parentheses, it takes one parameter oven Intertype notice we don't give a parameter name here and it returns a bull toe, actually, assign it. We just create the function body and for each of the parameters were passing in. This is where we put the name of the parameters. So we haven't int now we're gonna call it number. We can, in theory, call it whatever we want and different from a function. We're going to say the parameters. And if you have more than one, they would be comma separated the parameter in this block, and you can use it down here. Eso similarly to this function were saying if number is greater than three return true, otherwise returns false. And what that says is the Well, this whole thing is a function because we can call it like a function. The result of this should be a bull. So if we come over here, we'll see that the result that's printed out is actually the bull. Truer falls returned as a result of calling this function. Now. Not all closures similar rather exactly the same as a function needs to have a return value . It could just be. It returns nothing, which is void. And in the newer versions of Swift, this is represented by, um, just parentheses. I prefer void. This is still sin tactically correct. But boy, it is the same thing is nothing. So we can in here get rid of this and get rid of this. So why would we want to do this right? We can, let's say, have a function that's going to go and do something in the background for us. Maybe it won't return something. We can put that into a closure, which is is variable, and we could invoke it from somewhere else and our code and as a result, is kind of out of sight, out of mind. So it's extremely important that you understand this notion of assigning a function to a variable and being able to pass that variable around, opposed to defining one function at one place and having to access it from other functions and other classes. This makes it very, very, um, in computer science. It would call a very flexible and decoupled. You are making it flexible to pass us around and taking different values and do different things, So that's basically what a closure is. So if we wanted this on, let's actually go back to weaken. I'll show you this because we had it before we had a um, let's see. We had a question mark here before, and it was yelling at us about this because we're trying to call this this variable, which is indeed a closure and passing the number. But this is optional, so we can put this here right, and it's OK, but similarly toe unwrapping any optional. What you can do is you can say if let my rial funk equals this. You could do this because it's nothing more than something being held in a variable. Even though it's a function, it's treated just the same as something being a variable, and every other rule still applies of this because it's a far it could be mutated. We can assign another another closure to it enclosures, airfone and functions. I'm seeing them very like interchangeably, and they're basically the same thing. But the main thing to know is like a closure in a function. In the context of a closure is a function assigned to a variable and not this function. But anyways, because it's ah, it's a variable we can coming down here and it's copy and paste this Let's get rid of this and we can get rid of this. Um, and let's also get rid of that for the sake, And here we can do. We could basically reassigned this function because it's a variable. And that's kind of the point. I wanted to make that because the variable, all the rules still apply if it was a constant and it was let to my function, Um, obviously we cannot change this. And just so you guys understand, let me, um, let me actually call this function and show you guys a result of it. So if you give it just a second here, let me actually say a result equals this result two equals this. They're both gonna function the same way. And this is this is yelling at us because we haven't used this value. Um later on. But you can see that the actual function itself works in both cases. We need to change this to be a bull return true here again, and then return false here. And if we go and take a look at, um look at this, we can see that the true true or false, respectively, of the number that we pass in, Um, here and here they work it identically the same, but we can call it in these two different ways a key. Another key difference. Which I personally I like it better in the function situation opposed to a closure is when you call a function, you get a an auto complete. But you get the parameter name that is in the function before you actually type in the value when you're calling the function, whereas in a closure you can just literally pass in the name and actually doing something like this is not correct. And the reason it's not correct is a. In this case, it looks like this function, but also what it just expects and into in this case. But if we had, let's say in ends and another end, and we would say number and other. We can do something like that, and that's perfectly fine, too. But I hope that kind of gives you a clear idea of what a closure is, what the benefit of them are and a nice little intro to how to use them and create them. Um, I will stress that this is something that you're gonna have to do several kind of practice sets for and get a stronger understanding of it. It becomes pretty tricky. Once you have one parameter in a function as another function in which calls another function, it kind of becomes function. Inception. Um, yeah. I'm gonna end a lesson here. I hope this was clear as to what it is. How to use it. Please, Like comment. Follow. Subscribe. And I will get you guys in the next list. 14. Swift for Beginners Part 14 Strings: welcome back to another lesson in Swift for beginners. In today's lesson, we are going to be talking about strings, so we're picking up exactly where we left off. So let's get started by getting rid of all of this good stuff and let's set a title of here and, of course will like it strings. And I want to preface this by saying, I know we've been using strings already, but I wanted to discuss strings a little more in depth on this, but it will be a little quicker than the other lessons, but nevertheless, let's get into it. So a string is a way to represent kind of what it sounds like. Just some strings. So a text of sorts, it could be your name. It could be some characters. It could be anything, and a string is actually a data type so you can create a variable. And let's say it's called my and we can say its of type string and we can say equals a string. And we can put that string into double quotes like so, And this is how you represent a string or we can say, Let's say I don't know Let's call this Steve, then let's call the Steve Jobs. So that is a string. Pretty, pretty simple. Now, of course, this could be a string optional and, like anything else, could be an optional. We can unwrap this so we can say it. Flat name, uh equals Steve. And we can print down name and we'll see Steve jobs down here in just a second, like so. That's what a string is. So now, of course, a string and practical use because it could be used for a variety of things. It could be used for the title of a button. It could be used for a label on the screen. It could be used for an indicator saying that something is loading, so that's basically what a string is. It could be a variable or constant. It could be optional or non optional. But the last thing that I wanted to rather the main thing that I wanted to show was string interpolation, which is super cool. So a lot of languages has some type of concatenation function which, for those of you that don't know, basically means combining two strings together. So let's say we have we have first, which is a Steve and that we have last, which is jobs. How do we create a full name? Pretty simple and swift. You can basically dio this which I think personally is pretty cool. This is called string interpolation. String interpolation is the concept of putting a variables value inside of a string. And the way you do that is like this. So you have your quotes opening and closing for your entire string. And when you want to put a variable in the string, you put in parentheses in the actual strings quotes and you proved Preface it, you prefix it with a backslash and you do that so you can let Swift. No. The thing that's gonna be coming up right here is a variable. So if we actually print this, we'll see the full name Awfully cool. Now, I want you to also know that you can, of course, add in other text into here, and it doesn't only have to be two variables. What else wants you to know is it doesn't even have to be two strings. So let's say even in tier. So my name is Steve Jobs, and I am number number. If we come down here, we'll see in just a second. I am Steve Jobs and I'm number one. So the point of this is you can create a string and you can drop in values from other things that could be an integer. Let's say you are displaying thing some text saying You're about to purchase something and your price will be 1 99 You might want to show you might. You might want to show the string saying you were about to purchase something, and the thing that will be different every time is the 1 99 and you'll interpret Lee the price into the string. So this is how you can combine multiple strings into a largest drink. This is how you can combine its into a string, and basically, anything that has a value that could be seen could be put into the string. You don't have to worry about types in terms of some type. That isn't a string going into history, because you can interpret it. And that's kind of what I wanted to go over today. So, like I said, this is a pretty quick lesson. Become very comfortable with strings. They are literally used all over the place. Um and of course, there are a couple of functions and operations off of a string. So there's things like find and replace There is You can generate over the characters in the string. So it's actually show you before I end this video. So you do full named, uh uh, replace and you can replace. You can replace stuff that you find in a particular rains with another string. Every single class generally has some functions off of it. Now. I'm not gonna be running through all the ones on string, But there are a variety of ways to manipulate a string. You could turn it backwards. You gain replace a particular part of it you can fought. You can check if a string, um, has any characters. You can check a string's length. Let's say you want someone to input a password and it want. You want it to be at least eight characters. You can say something like, I believe it's length. I could be mistaken. Ah, it's count. So you consider account of the string, which is the number of characters in the string. So I'm gonna digress there. There are a variety of operations. I encourage you to go and take a look at them. But that is basically a little bit more in depth about a string. I hope you guys found this lesson informative. Leave like, follow, subscribe. Comment more than happy to help you guys out and I will see. 15. Swift for Beginners Part 15 Arrays & Dictionaries: welcome back to another video in our Siri's swift for beginners. In the last video, we took a bit of a deeper look into strings, and today we're gonna be taking a look at a raise and dictionaries. So let's go into here and let's actually get rid of everything that we had. Let's change this title up here from strings to a raise and dictionaries awfully is about that, right? Dictionaries. So Narain a dictionary. Both are a type of collection, and the name implies that it is a thing that holds one or more other things. So the first of those things are raised in dictionaries are both data structures, so pretty fundamental to computer signs and hence fundamentals of swift. So an array is an enumerated collection of one or more items, so you might have a collection of strings. It could be names Joe, John, Jacob. It could be numbers. It could be other objects. When I say enumerated, all that means is it's in order, Um, numerically zero and upward. So, uh, what? I wanted it and I've explained here whether I'll do it with examining example is if we have names to create an array. We can just say open close square brackets that we can put things in here. So, Joe, John Jacob, that is that these are comma separated. So that's how you separate out things that you put in your right and this is in direct. And I meant what I said enumeration. What I meant is the values in here are indexed zero and upward. So Joe is value zero Jonah's value. 1234 If we had more onwards and you know, endless, it just keeps going and going, Um, before we get the dictionary is what I want to talk about here is typing with a raise. So as you can imagine, because Swift is strongly typed language, we need to give everything a type. So what would the type of this Arabi array itself is not a type, right? What we need to do is me to say this is an array of what This isn't array in this case of strings. So the type of this is string in the square brackets. Now, what if we change this to numbers? That's 812 three. This would now be an array of int. What if we want more than one type of thing in an array. What if we want something like Joe? Um Ah, 123 So now we have instant here, strings in a double. So now what we can actually do is we can change this toe any and that's perfectly fine as well. So what I want you to know is array itself is not a type. An array that holds a particular type is the type of that array this Arrese, a array, an array of anything. Whereas this array is an array of int. So that's basically an array you can literate over the elements in an array. So when we actually if you recall from our four in loop session what we were doing there waas we would do something like this for number in its called this numbers for number in numbers print number. If you go down here, if you give it a second, we'll see our numbers. And this is the notion of iteration where we go over every element in the array and that's basically an array for you. So similarly, I want to show an example of a dictionary. The dictionary is very similar with one key distinction. So let's go with, um, cars. So in array, while we have everything linearly, comma, separated a dictionary instead of just having values like this will have keys and values, so a key will point to a value. So let's say we have the key being the type of car and the value being how many cars we have of that type. So just for sake of example, it's a BMW three on, uh, one Lexus 12. This is an example of a dictionary. The reason is called actually dictionaries, because when you go look up, a word in the dictionary has a definition. In this case, the word is a key in the definition is a value that it points to in front of the colon. So when you go on like a BMW, you get this similar to an array. The type of this is not just dictionary notice that this is also in square brackets. The only difference here is we have a key and we have a colon and a value between every comma. The type of this is a string into dictionary. So we have a string is a key and the expected value for each of the strings is an end so very, very similar. The beauty and in a dictionary is you can look up stuff much faster. You can say Hey, give me the value for the key BMW, whereas let's say there is a key in here or a value in here called BMW. To find it, you would have to go through all of the things in it one by one to find it. And that kind of lets us get into the discussion of performance, which is a little out of scope for this particular lesson. But this is the notion of an array, and this is a dictionary similar to Iterating over an array. You can also eatery over a dictionary. So let's say this is Well, let's just keep this those cars we can say for car in cars, Prince car and down here we'll see that each one each thing. That car is the key in the value itself. Optionally you can actually say for key and value in cars print the key and down here we would a second. We'll see that we have the keys, and if we just print the value for each of them. We should just get the numbers in this case and we have the number. So raising dictionaries are very similar. They are very, very, very commonly used. So I strongly encourage you to understand them. Ask questions about them, read up on them. If I can be of any more help, I, um more than open and welcome. Any feedback, comments, suggestions or anything you're struggling with? Um And I'm gonna leave the lesson right here. And if you guys found this video informative leva like follow, subscribe comment. I will see you guys in the next last theme. 16. Swift for Beginners Part 16 Ints, Floats, Doubles: thighs. Welcome back to another lesson in Swift for beginners. In the last video we covered a raise and dictionaries, and in today's video we are going to be diving a little deeper into numbers, and we're specifically be covering integers, doubles and floats. So let's start by getting rid of all this array of dictionary stuff. And let's just give this a title of numbers. So what I want to explain here is the fact that there are three types of primitive numbers in computer science and hence in Swift. And I'm gonna do this with an example. Great. Let's say a is one B is one dot there is zero and C is one. Duh. There there's there's there's there's there's there's one. So the difference here is this is an integer. This is a double, and this is a float, an integer in computer science and in most languages has a maximum number limit. A double is a integer with two decimal places. A float or a floating point is a number which can have many more decimal places. There is, in fact, a limit to this, but generally no one ever hits that limit. The reason you might want to use one versus the other is as follows. If you want to represent someone's age, maybe it makes more sense to do it as an integer. If you want to represent a price that something might cause, let's say 1 99 you might want to do it as a double. If you want to represent on your iPhone screen the with of a particular box, you might want to do it as a float. So it's important to understand the difference of these and the fact that these are three different types, in fact, So this is an event. This is a double, and this is afloat, and it's important that you understand that we cannot do something like a plus B, and the reason that we can't do this is because they are different types. So how do we go about doing this? Because A is an integer and B is a double Well, one option is we can say we want to convert B to an end and then added to a But this might still be yelling at us. In this case. It's not because it knows that this is a double and can be converted to an end. But if they this was a different type of value, this conversion actually returns an optional. Similarly, instead of converting the double to an end, what we can do as we can convert the end to a double My point here is you need to understand. While these are all numbers, a number is not a type type is an integer or a double or a float. Similarly, all of these convey of type optional also. So let's say we wanted to get access to the actual value of B. We could do something like if let value vehicles be coming to here and we could do a print of value. And then here we'll see the actual value. And if we if you notice, actually, even if we put stuff in front of us in our print, it'll only show us while in this case shows us all of it. But if you were to actually use is in the application, you'll actually have the number be cut off after two decimal points. That's the most important thing you need to understand. With that being said, I'm just gonna and the video here pretty quick lesson, but nevertheless very important. It's important that you understand the differences between these numbers. When you look at code in a professional setting or write something yourself, um, leave a like comment like follows subscribe more than happy to help, furthermore, and I'll see you guys in the theme. 17. Swift for Beginners Part 17 Libraries & Frameworks: back to another lesson in Swift for beginners in the last lesson recovered different types of numbers. So doubles floats in its and today's lesson. We're gonna be taking a look at frameworks and libraries. And let's get started by getting rid of all this stuff from last lesson, including this comment. So I'm sure you noticed for the past several lessons we have this import foundation appear . So what the heck is us on? I've kind of been purposely ignoring it because I wanted to dedicate a video to this. So the old command and click on this you can actually click on jumped to Definition and you'll get this monstrosity here. But if we go back basically what a framework in a library is in computer science and again and hence and Swift is a group of functions, classes, variables and just stuff in general that is organized for people to include an import into their files as they need it. So in IOS, there is a friend recalled, you I kit so user interface kit that includes all the user interface stuff, so stuff like but end images, text controllers, switches, etcetera, etcetera and they organize it for the sake of sanity and being able to find stuff because A Z could imagine people of billions of lines of code, so it needs to be organized. So at the top of any of your swift files, as we're going along, you'll see a variety of import statements. So let's say we want to create a but in here and the class would be a u Y button. But if we start typing, um, but and we don't get it, why don't we get it? And the reason we don't get it is because that particular framework is not imported. So we had to say, Import, you like it. And now if we start typing in the u I. But in notice, we get a bunch more stuff. But we also get what we want, which is a you I but in so things are included into frame recent libraries and the way that you're gonna be able to find out where something is when you need it is eyes by a little bit of research, a little bit of experience and a little bit of the naming convention. So generally things are named in a way where you can kind of read it and estimate what's included. So this brings me to the second piece that I wanted to talk about, uh, which was naming conventions. So an apple land Apple likes the name things kicked. So if we haven't, if we have a framework with functions and classes that deal with in APP purchases and transactions, that stuff related to, like a store, right, like you're selling stuff, your shopping cart, etcetera, etcetera. So the framework for that is called struck it. So everything is a kicked, um, and take that with a grain of salt because it really isn't everything, but it is a very popular naming convention. Another common thing that people do is they like to alphabetize er import statements. So f s U In this case, that's something important to know. Of course, these are all libraries and frameworks that are written by apple and included in your environment to import. But you can certainly go and import other other libraries by third party companies as well . And the popular example that I like to give is add mob. So add mob is the Google's advertising framework, and I'm sure in a lot of APS you've seen a little banner at or a full screen ad pop up with a video or something like annoying music or something play, and that's driven by Google's network of advertisers. But hold on. This is Apple's environment apples codes aware had his Google fit into all of this so you could have to go to Google's developer website, and they have a whole guide there of how to download their code and included into your code . The long story short is you basically bring their code and you could do something similar to import. Google Mobile adds. Now, right now, it's gonna yell at me in, like, 10 seconds right right there, because this doesn't know what this is, because this this code does not exist in our particular environment right now. But I want you to understand that you can bring in first party. So apple made frameworks and libraries and also third party, and it's very common to bring them in through something called cocoa pods, which we will be covering in a different course, seeing as this is a beginner course. But that's what this import appear is for all of you that have been wondering So foundation you can imagine has things like strings and integers and floats and doubles and so on and so forth. So that being said I'm gonna and the lesson right here, I hope you guys now understand what this important statement is appear. Leave a like comment. Feedback is appreciated. Subscribed. Follow. I will see you in the next lesson theme. 18. Swift for Beginners Part 18 Naming Conventions: back to another lesson in swift for beginners. In the last lesson, we talked about libraries and frameworks, and particularly what this import statement is in today's lesson, which is gonna be another shorter lesson. We're gonna talk about styling your code and what the common convention is so generally when you name something. And let me also preface by saying that I have kind of mentioned this along the way in other lessons. But I wanted to make a dedicated lesson for it, as it's pretty important and people are pretty biased about it. So when you create a variable, you have an option in terms of naming style. So let's given Let's give of our Let's say we have a variable and it's gonna be number of bank accounts. There are two different common styles to writing that out. One is called snake and one is called camel. So a snake case would look like this. So notice that kind of, I mean, it doesn't look like a snake with the nature is you have these underscore separating everything, and this is called ah equals an empty string so it doesn't complain. Um, but this is the nature of a snake case. Now we can also do this, and this is called Camel case. And the reason is called Camel Cases, because the first word is lower case and everything after that is upper case. I guess that signifies the hump on the camel's back hum. I don't really know why it's called Camel Case it. I'm assuming that's the reason why it's, I think that's the reason everyone assumes why thes are kind of computer science agnostic styling standards. But I wanted you to be aware of them. So when people have discussions in professional settings of naming conditions and styles and standards, every team has their own standards. I want you to be aware of what they are so similarly to naming variables and snake or camel case functions can also be named in sneaker camel case. So let's say, is larger than five or is larger on five. Another important thing that I want you to know about naming in general is classes, struck's and protocols, which at this point we've seen all three are usually all capitalized. In other words, rather, the first letter of each word is capitalized. The whole thing is not capitalized. So if we have a class called Big Person, it would look like that. It would not be snake. It would not be camel. This is generally a good naming convention and standard. Ah, that way you can look at something if you create an instance of Joe and you say it's a Joe is a big person. We can know that this is a class by just the way it kind of looks. And as you started using these standards, and you see those standards are not consistently used, it's going to start to irk you as much as it hurts me and other developers that I know. So that's about it in terms of standards and naming conventions. And I wanted to go over if you found this video helpful. Please do leave a like comment, subscribe, follow from more content and I will see you in theory, 19. Swift for Beginners Part 19 Ternary Operator & Return Statement: back Teoh. Another lesson and swift for beginners. In the last lesson, we went over styling and naming conventions, and in today's lesson, we're gonna be going over some or code. Specifically, we're gonna be jumping into the turn Eri operator and talking about the return statement. So let's start by getting rid of the stuff and let's go. We're going to start with the theater Neri operator and then talk about the return statement. And, uh, yeah, we've used a return statement before, but we're gonna discuss that a little bit more so first and foremost, what the heck is a turn? Every operator, so a turn. Every operator is a bit more of a shorthand way to do an if else statement. So if you recall well, let's actually stopped reading. A function is bigger than five. I'll say, has a number and it's gonna it's gonna take a return. A bull. You see, if number is greater than 53 in the return true house for dinner, Return clips else would return false. Almost put a return False on Here is a default case, so this is ample, actually discusses turn every and return statement. So maybe We'll, ah, lady will do both. So here what we're doing is we're using an, if else condition to return in a true or false right. If you were to write this in a more shorthand way, let's in one line. How could you do it? We can say let results equal number greater than five. So if the numbers greater than five, it's true. Otherwise it's false, right? And then we could do here is we can say return results. So this is a very similar way of saying, If else so all you're doing is you're instead of having new lines and curly braces, you're using this question mark and this colon. So basically, you're saying if this number is greater than five, the return result here is gonna be equal to this. True. Otherwise, they will be equal to this false. Now, let's take this one more step further and that'll actually transition as nicely into talking about the return statement. What if we just did this? Why even have a results notice we took our CO. That was about five lines, and now it's one line. So what we're saying is return either true or false conditionally on if this number is greater than five, if it's greater than five return a true otherwise return of false. But what is his return Doing so moving on from Turnberry to this return statement? This return statement is basically just saying that this function, um, here this function is saying that it wants a bull back. His return statement is saying, Here you go here. Is that bull either true or false? It wants something back. That's of type Bull, that's all a return statement, really is. It is the thing that hand back a particular variable or value of a given type that is expected or asked for. That's what this cured return does. So I don't have used returned quite a bit. And maybe I should have ah, explain this little early Iran, but I figured, um, seeing it seeing it in examples would help explain it clearer than any other way that I could explain it so so eternally operator to come back to that. Is your condition full about of the question mark? And this is the This is not to be confused with an optional This has nothing to do with an optional even though question marks also used an optical notation. If this condition turns out to be true, if this condition matches whatever you're checking here, do this thing otherwise do this thing. And in this case, we just want we want to say there is true or false and we want to return. So we want to return the result of this statement, which is gonna be one of this. So let's see you one more example and we'll end the lesson there. So let's do funk. Um, random number. And it's not gonna be random. In our case, we're just gonna hard coded. But I want to show you the show you the difference of using, like, an end instead of a true or false. So let's say if we generated a random number, let's save our exes. Five. He's a return. If X is greater than three, we want to return. Sorry. We want to return 12. Otherwise want to return to so notice. I myself in a little mistake. We already have this return statement here. This is just yelling. Get us that var were saying far X but we don't actually change the X. So we could make this a let. That's why the line got highlighted in yellow. But basically, this is very similar to the one above that we have. We're saying return. Um, either this number, this number based on this condition. I also want you to understand that even though that we are combining returns and turn Aires , it doesn't have to combine. Right. We can simply use Attorney ary on its own and get the result out of it. And attorney doesn't necessarily have to presume create a result. Either We're making an assumption. We're presuming that it has a result, but it doesn't have to. This could be like if this condition were to hold up, call one function, otherwise call another function. This is very, very similar, if not identical to an if else and its functionality, but just much more short handed and more concise, which is kind of a more swifty way to do things. But that's basically the notion behind a return statement and eternally operator. So I'm gonna and the lesson right there. If you have any questions, don't hesitate to leave a comment like the video follow. Subscribe and I will see you guys in the next lesson theme 20. Swift for Beginners Part 20 Typealiases: another lesson in Swift for beginners. We will be picking up right where we left off, and in today's lesson, we're gonna be discussing type aliases. So let's get started by getting rid of everything that we had here from the last lesson. And let's give us a title of type bilious. So I wanted to explain what a Thai police waas and more importantly, why it's eso useful in my tell used in professional code bases in an industry, so a type bilious is a way to give another name to given type. So let's say we have a code, um variable and its other type string, and that's what it points to. But let's say we are passing this around as a parameter in a function called It's a Valid A Code and we have a code on this. So in this case is kind of makes sense, right? We can just do validate, we can pass on the code, and this is pretty easy to read now. What if we wanted to make this string say code instead, somebody tell you what that means so you can actually do something like this. You can say type bilious code is a string, and here we can say food and code. So what we've essentially done is created her own type. And we've said the new type that we're creating is called code, and it's a string now. What the heck is a used usefulness in point of this? Because it seems like you're just renaming the string to code. Um, before we get into that, I want you to understand that this string still exist. You can still use it. You just created another usage of it. So the reason this exists is for more complex types that might be a little more confusing to follow. So let's say we have a So we went over dictionaries. Let's say we have a dictionary that's going to represent somebody's home address with a key might beast like Street, City, state, country. And the value might be, respectively, the values for a street city state. So let's say, New York, New York, United States, right? So how would how would that look? Let's say we have address and the type would be, um, it's string to a string in a dictionary and let's just leave it empty for now. But just to prove the point. Now, if we have a function here called Valid it Address and we pass in this address and we need to say this is the type now this type is a little less pretty than just having one thing be a string. So how do we resolve this now? What we can do is make a type bilious and say an address is a string to a string. And in here we can say this is an address. So this is an example of a type used to simplify the underlying type. So the type billions that we've created is address. The underlying type is a dictionary of a string key to a string value. But now we can just say this parameter type is an address, because under the hood we've specified what addresses. So let's do one more example in a that used commonly. So we talked about closures. And let's say we have a closure that takes in a promo code and validates it with some some network hole. So, for example, let's say you have a coupon code and you want to enter it into the app, but you want to check if it's valid. So you might have, let's say validate function and its type is gonna be taking in a string lips. So we want this to take in a string and return a bullets we could optional. And let's say we have another function called, Um did enter code, which has a code now in here. What we can do is if this actually had something assigned to it, we can do something like and then in here we can pass in the code and the result of this would be true or false. Now, what if we want into make this whole thing look a little nicer? Because this is this is a little tough to follow. So what I mean by that is if a new developer engineer comes into the code base, they're gonna look at this and be like, OK, there's this variable called validate function and it's a string and it returns. It takes a string and a return to bull. Okay, it's also optional, but maybe this isn't as cognisant of what this is actually doing, so there's a better way to do this, and the better way to do this is using a type bilious. So what we could do is we can take this whole thing and we can create a tight billions up here and we can say promo could validation function is that type and that we can say this is that so this is a much more cleaner and prettier way to right, And this is how it's actually done A lot in professional code bases. Um, this type is a alias. Basically, a alias appoints to this type. But using this to declare the type of this validate function is lot cleaner than using this . So one reason we do this is for read abilities, point readability, readability sake. And the other reason we do this is, let's say we have multiple functions are multiple variables closures in this case that need this type. It's a lot easier and more clean toe. Use this type that we've created in the Thai bilious rather than this type. We can see here very clearly that the point of this closure is first, we can see that it will be a closure because of the types name. It's gonna be some type of promo code validate function that we can also see kind of the point of the function that should be assigned to the US Um, and the usage of it. Now, the other cool thing is in the larger code base. If you didn't know what the signature of this function was, in other words, what parameter types it took and what it returned, you could holds command. And you can click into this and the hit jumped the definition, and it'll actually highlight in the code wherever that type aliases defined, and that goes for different files to. So in our example, we're in just one file. But if you have thousands of files, you can commend, click into it and then hit this and it'll jump to where it is, and it will show you. Exactly. Lastly, before we end this lesson, I wanted to mention generally the type Ilyas is created right above the code that uses it. So in this case, we have this variable that is using this type, and we've put the type of lives right above it. So that's generally a good way to do it to keep things organized. Um, and just for the sake of easily following where the heck this type aliens came from. So I hope that clarified what type bilious is and why it's useful. Um, and again, I just wanted to stress it's used pretty heavily, so I would I would understand it. Become comfortable with it. Yeah. I hope you guys found this informative. Leave it, like follow, subscribe, comment for any questions or 30 and I will see you guys in the theme. 21. Swift for Beginners Part 21 Math Functions: welcome back to another lesson in Swift for beginners. In the last lesson, we covered type aliases and in today's lesson, I wanted to go over math functions and a very, very brief introduction to Rikers in Let me preface this by saying, Rikers in can become a very complex topic. But I figured I'd be very interesting for you guys to at least get a very brief overview of it. And should you be more interested and leave comments and I get feedback, I'd be more than happy to do another course just about Rikers in. But let's get started with the math functions. So let's delete all of this, um, and create a variable X and make three. Ah, North Korea variable y and make it to So like any programming language. Swift, of course, supports a variety of math functions and what we can do to model it out, it just go through them really fast. So of course we can add things together so we can do X plus y. And over here we get a result in just a second. Cool. And of course, we can also do, uh, X minus y we can dio X divided by Why, If it's last one. Weaken dio x times y so multiplication division, subtraction and addition, pretty basic concepts. You could do all them pretty easily. The important thing to know here is these air both. It's right there, both of type integer. Now we haven't explicitly put it here, but Swift has inferred it by what we're assigning toe the variable X and the variable. Why? To perform math functions on two variables. They must be the same type. So if we make this float with a bunch of decimal points, it's gonna start yelling about all of these math operations. Because again, Swift is a strongly typed language, and they need to be the same. So those are your basic math functions. They're pretty heavily used, um, in different calculations, as you can imagine. Now there are math operations to get, like pie and round numbers up around numbers down the instead of running through. All of those is there are a ton of them. I wanted to show one specific one that's pretty pretty cool and used more often than one would think. Let's start by making, um, this 30 and let's start by making this seven so And that operate is called the module. Oh, operator. And what we're gonna do is we're gonna save our result in six percent. Why? What this is basically saying is give me the remainder after you've tried to put why in tow X as many times as you can. So what does that mean? So why is seven and ex austerity Seven can go into 30 four times before it can't go in any more evenly. So seven and then 14 and then 21 then 28. But if you add another 7 to 28 you're gonna go above 30 so seven can go into 34 times and there is a remainder of to. So the result here is to and why? So what is this kind of useful? So the modular operator is often used to see if something is either even or odd or divisible by another number. So let's say you wanted to find every even position or every odd position in a list of numbers. You can say if result equals zero, you can continue. So what this is basically saying is if the why can evenly go in tow. X with no remainder. Let's give an example of out say, like, 10 10 can go into 33 times and there will be a remainder of zero because it's it is divisible by 30 is divisible by 10 evenly on I mean, it is a It is divisible by three. The 10 33 so you can go into here otherwise, if it's not, do something else. So this is a commonly used operate, and, um, and it's pretty cool if you actually wrap your head around it. And once you start seeing in the wild, so say, But that's what I wanted to go over and math functions. Now let's just take a very quick look at Rikers Shin, um, and will end the lesson after that. So Rickerson is the concept of calling a function within a function. So does that mean So let's say we have a function called ad and it takes in two numbers and let's say returns number. Rickerson is a notion of firstly doing the operation that you want to do in the up in the function itself. So in this case, we just want to add the two numbers and then calling the same function Well, right now, it does not want to cooperate because this is incorrectly spelled. Let's just return zero down here to make this happy and cool and do that. So rickerson is a concept of calling the same function that you're in a recursive lee over and over again to get to a particular result. So to preface, I mean again, I'm going to go over this right briefly, as this itself needs a nice to our course to fully explain. But imagine if you had a checkers board like a board game, like a board game for, like, checkers. If you wanted to find all the combinations that a person can move their peace to do that one by one would be insane and would be not. It would not be practical. So Rikers Asian would basically be very useful in that situation. So the the notion is you would start a particular square, and you would Rikers through the same function and go toe every single possible next square from the given start point, and eventually you're gonna hit the other side of the board. And that's where your recursive function stops. So rickerson is the notion of iterating through a large amount of combinations to find a pathway or to find AH, particular thing you're looking for. So another riel role example is Imagine your Facebook friends So Facebook when they interview candidates for software engineering positions and swift Aziz. Well, well, they'll ask for is a lot of Rikers in questions and Rikers in based theory. And the reason they do that is, if you can imagine if you if you try to draw out on a piece of paper dots representing people, you have several dust that connected, which are your friends, then they have several dots that connect to each of them better their friends. So let's say you want to find how maney how many people away rather how Maney mutual friends sit between you and another person you can recursive Lee go through all the connections that you have as friends and their connections, and so on and so forth until you find that said person. So rickerson is a pretty powerful algorithm, actually, that when applied properly, toe large sets of data can yield really, really cool results. And again, the notion is calling yourself this as in the same function from within that given function . And there is something called a base case, which is you wanna have a point in the function where instead of calling yourself, it will start returning or stop calling itself. And the reason you want to do that is if you don't have a base case where this is the real , this is the point where the function stops. It's just gonna call itself like a limitless way, in essence, just becoming an infant infinite while loop. So that's the nation nature of Rikers in. So I hope that brief introduction made sense to you some degree and again leave some comments on reach out if you want to learn more about this. And I was certainly be happy to make a course on it. Other than that, we went over math already. And that was Rick. Urgent. So I'm gonna end the lesson right here. Leave her like if you enjoyed the video comment, follow, Subscribe, and I will see you guys in the next left Theo 22. Swift for Beginners Part 22 Searching & Sorting: welcome back to another video in Swift for beginners. And this lesson we're gonna be quickly giving you an overview of searching and sorting. So let's get started by getting rid of all of this. And let's ah, let's discuss this a little bit. So the nature of surgery in starting is pretty important in general, in computer science. Um, what I mean by that is, let's say you have a array or a collection of numbers, and you want to find if a given number is in that array, maybe you'll want to search that area to find it. Maybe he's also a good idea to sort that array in ascending order. One toe, Let's say 10 and that'll be easier to use to find it. So searching and sorting have a variety of algorithms, that air that have been made over the past several years and computer science. And I just wanted to introduce the concept here, seeing as it's a beginner's course. But this is a topic that is heavily asked in interview questions, and I encourage you to go look at various searching and sorting algorithms online, and other, of course, is so we're going to start by creating an A rag of numbers. This is gonna hold a bunch of numbers. Let's start giving some variety in here. And what we want to do is we're gonna write a function that's gonna return true or false if a given number is in this array. So we're gonna write a function, um, is number in array and it's gonna take a number. What does it mean? End and it's gonna take a array, which is gonna be an array of instant. It's going to return a bull and down here we're gonna call this function, um, with any given number. So the default this function will return False. Right, Um which basically means if we haven't found it at this point, there's nothing else we can do, so just returned False. What we want to do here is use a for in loop. So what I want to show here is a very basic way to do a simple search which is basically iterated it In other words, go through one by one each element and try to see if that element is the number that we're looking for. And there are a variety of searching algorithms by just one introduces to you. So we're gonna do for, um, let's say, X in array if x equals number turn true. And that's basically our searching algorithm, so it's very basic. But what we're going to basically be doing is for each thing in the array, which is our input. And I just called the thing X. If you recall, we can call us whatever we want. If that thing is the number that we want, which is the other parameter, which is an int return. True, of course, if it's not, it will keep going. And, um, something important to recognize here is, let's say we are looking for three and there are multiple occurrences of three in here the first time it encounters what we're looking for. Ill return. And it won't even bother going down the rest of the rest of the array. So let's ah, let's see this in practice. So we're gonna say is number. Okay, lets see. It's not wanting to auto complete. Let's do this. Okay, is three in numbers and if we come over here, we'll see to give an output. Now let's see if we put some other number that isn't in the array will get a different output. And that's basically how surgeon works. Weaken. Basically, search. If something is given in a larger collection now, why is this important? So let's say you have an email app and you want to delete a particular email, but it's in multiple in boxes and folders. Maybe you have the emails in an array, and you would wanna basically cycle over and search for all the occurrences of that email so you can delete it. That's a real world example of this so similarly for sorting. Now it's important to note that this array, for the most part, up until, like this point, is in ascending order. Now. Oftentimes, your collection of data won't be in ascending order. It might be 12 and one and two and 13 and zero and three and four now. This brings me to the point of why storing is important. If we know that a given set of numbers is sorted, it makes her lives easier. So if we if we know it's in ascending order and let's say we're looking for five, what we can do is we can an in essence keep going through the elements one by one until we find something that's either five or something that's greater than five. For example, if we find a six at that point because we know the numbers are in order after the six week , then surely can't be a five. So at that point we could just return falls and say, Hey, I got to a six and there is No five in here. Therefore, there is no five in this collection. Similarly, what you can also do if the collection of numbers assorted is, you can start in the middle and you can say OK is what I'm looking for, larger or smaller, and as a result, you can either start going through the numbers in here to the left or to the right. And what I just described is a searching algorithm called binary search looks. I can't type today, so the nature of being called binary searches You start in the middle of a given array, and you either go left or right. Right? So let's say we go to the left. That's all we know. Five. A smaller or like to is smaller, so we'll go here we'll start in the middle here and be like, OK, it's too gonna be on this side or this side, and we know it's gonna be on this side. And surely there's are, too. So that is the nature of sorting and searching and why they go hand in hand. Buying a research requires that a given array of numbers assorted because if it wasn't sorted, um, the the notion of going either to the right or left based on if the number is larger or smaller doesn't hold up. So that's the very basic intro to searching and sorting. I hope that was, ah, decent explanation. Let please leave a comment. If you want me to go deeper into this, I think searching and sorting and algorithms themselves will require a whole course of their own. And I'd be more than happy to go through that with you that that is something that we I like to cover quite a bit at my current, uh, employer, which is Microsoft. And that's what we interview with quite a bit. So yeah, light comment follows. Subscribe on. I'll see you guys in the 23. Swift for Beginners Part 23 Wrapping Up: Welcome back to your final lesson and video in Swift for beginners First and foremost, if you've gotten all the way through huge Congrats to you. We've covered quite a bit of material and I wanted to take this opportunity to write out a little more complex code and apply a lot of the things that we've learned. And yeah, with that being said, let's get started. So we actually picking up right where we left off. So let's get rid of this and what's called us a summary. What we're gonna be doing today is creating a representation with classes of a neighborhood . Cool. So what does that mean? So we're gonna have a class called neighborhood. It is gonna have. Ah, and a ray called Holmes. What? You're gonna be types of homes and we need a have a class called home. And what else do we need? So each home has rooms, so we wanna have an array of rooms, which is gonna be an array of the room type object which will create down here and so on and so forth. So what do we have going on so far? So we have some classes we have some constants that are of type array of either home or room. What else can we do? So let's say each house has a protocol called my address. So let's say we create a protocol. Appear, Um, let's call it address data sores. They'll have a function called my address, which takes in nothing that returns a string. We're gonna say each home implements rather conforms to this protocol, and we're gonna return 23 Main Street. And that's a protocol that we just added. Nobody got classes. A rays and particles were also using the string. Here we are also gonna define a variable, rather a bunch of variables on each of these rooms. Actually, we're going to save. Our color is Blue Bar with is 12 of our height. Other let's do blank is 32.5, so it's a fairly large room. Um, let's add some functions. So let's say is on second floor with returns of bull lessons returned false. So the point of me go into this exercise is to really exemplify. This is complaining because we don't have an initial Isar. So let's fix that. Rather, that's actually complaining, cause we didn't initialize this, which is okay for now. But rather the point of doing all this is Teoh. Emphasize how we can combine all the fundamental building blocks that we've learned to create and model something in code quite specifically is. In this case, let's say we wanted an app that modeled, um, a neighborhood. Let's say this is a part of Google Google Maps, so we would have a class represents a neighborhood, and it would hold on to an array of, let's say, homes and each home would hold on to an array of rooms and so on and so forth. Now we can have a protocol that we used to ensure that every instance of this home class represents this protocol and respectively conforms to it with the function. We are using its down here and notice that we have this height, which is a double. So let's say we wanted to get the area of the room we wouldn't need to do with times height . But if you wait a second, you'll see. I mean with times length. Sorry about that. You'll see that this gives us an error because these are two different types. So what we want to do here is we want to take this length and wrap it in int If you recall we, um we went over this Nate, this notion of castings and now we have an array. Uh, rather, now we have area. We can say an area is a and optional. And let's say we wanted to say get area as a function and return and it's in here. We can exercise our knowledge of unwrapping an optional with a guard statement so we can say guard value equals area else. Return blue eyes. We can return value on and so on and so forth. So I really hope this helps Emphasize Sorry if that's about the let in here. Um but I hope this emphasizes ah, the power that we now have to combine a lot of the components that we've learned and create a sophisticated class and structure and objects and object It opted oriented design. So before I end this video and again congratulate you for getting through the entire chorus , I wanted to give a little bit of ah where to go from here. So from here, I would strongly encourage you to practice this in text, which is actually writing code. The more you write, the more it'll be committed to your memory. And I think that's the best way to learn personally speaking and also having taught others . I would also encourage you to take a lot of this and go and go through a I a West course, and I will be actually personally providing one. Aziz Well, and once you go through that course, you'll see that. Of course, of course, would be in Swift. But you'll see that, Ah, lot of this and a lot of what we've learned will apply to Swift and in ah, Brother will apply to IOS, and what you'll get out of it is you'll basically just be importing, um, other libraries and frameworks and using bits and pieces of that framework to create stuff . So as a simple example, if you wanted to create a but in on an iPhone screen, you would create a but in and it takes a frame, which is the X and Y position and the size and the heights of the actual, but in so we can say 00 100 wide and 20 high, and this is how you would create a button on an iPhone app. So I hope is getting clear that all the swift that we've learned is extremely applicability to IOS develop and rather any development that is done in Swift. So here we have this you I but in class, which represents a but in it gets initialized with a frame, which is a CD wrecked, which is a core graphic.