How to Create iPhone Apps - Speed Edition ✅ | Grant Klimaytys | Skillshare

How to Create iPhone Apps - Speed Edition ✅

Grant Klimaytys, Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
15 Lessons (1h 12m)
    • 1. Introduction to Swift 3

      2:30
    • 2. How to Setup Xcode 8 and Swift 3

      2:54
    • 3. How to Setup iOS 10 Simulators

      1:36
    • 4. Setup a Playground in Swift 3

      2:18
    • 5. Variables in Swift 3

      7:17
    • 6. Collections Arrays in Swift 3

      7:37
    • 7. Collections Sets in Swift 3

      4:21
    • 8. Collections Dictionaries in Swift 3

      4:50
    • 9. Logic If Else and Switch Case in Swift 3

      5:58
    • 10. Round and Round Loops in Swift 3

      7:07
    • 11. Functions in Swift 3

      6:48
    • 12. Optionals in Swift 3

      6:29
    • 13. Classes and Objects in Swift 3

      6:08
    • 14. Inheritance in Swift 3

      4:27
    • 15. Summary of Swift 3

      1:15

About This Class

Ever wanted to make iOS apps? 

Ever wanted to get a higher paying job?

Well it all starts here! I will teach you everything you need to know about app creation in a FUN and NOT BORING way :)

Enrol now and you'll have 3 of your own apps by the end of the day!

Transcripts

1. Introduction to Swift 3: Swift three is Apple's shiny new language for creating APS on all their devices. As you might guess, it's now in its third iteration. And before that, we had swift to just plain old, swift and objective see way back at the beginning. Now a projective C code isn't compatible with swift code. There are ways you can use Objective C libraries and code inside of a swift project. That's fine, but you can't mix and match the two coat types to a certain extent. The same is true between Swift 12 and three. Things changed between versions, and every time you upgraded to a new swift language, you had to change all off those references that changed inside of your code, which was a bit annoying, but worth it to get a more mature language in Swift three, Apple have said, or the swift organization that takes care of the language have said that Version three is the last time they're going to have any breaking changes. So your code from this point on should be fine with future versions of Swift like Swift for five, however far they're going to go. Okay, All that means is you now have stability in your code, which is a very good thing because you don't want to come back and have to change a whole bunch of lines of coat. All of that is great news because it means everything you're about to learn isn't going to change in this section. I'm going to teach you the swift language and its basics. Those basics include variables, loops, arrays, functions, classes, objects, operators and inheritance. Of course, there is a lot more to Swift three. But these basic items will see you through most situations and help to give you a clue when you come across various of a concepts that you don't know a lot about. So by the time we're done here, you're going to have a really good understanding off the swift language, and you're going to have all of the tools that you need to make your APS 2. How to Setup Xcode 8 and Swift 3: setting up X Code eight and Swift three couldn't be easier these days. Simply jump onto Google and search for X Code eight beater, and the first result that you going to click on is the one from developer dot apple dot com . And in order to download Exco Date seem to click the icon and Apple will ask you most likely to log in Bear in mind. This could change, so I'm just going to log in and be back in a second. If you don't have an apple, i d. For the developer programme yet then you can get one and you don't have to pay until you release APS. So the developer account is free. The ability to release APS is not on its something like £80 of gear in the UK at least. Okay, so just lock in and be back in a sec. And once you're in, you're presented with a list of downloads. We only need one in this list, and that is the very top one, which is the X Code eight beater. Of course, if you're developing for other platforms, then you'll need the other things like Mac os IOS 10 and watch us. But X Code eight contains pretty much everything we need. So let that download and come back when it's done. Once complete, unzip the file that you just downloaded, and you should get this Exco dash beater out of it. Once you have it, simply click and drag it over to your applications folder. Drop it in there and let it in store. Once that's complete, you're going to run it, so I'm just going to run it here as X Code dash beater. Now, if Exco Date is already released when you're watching this, it'll just be the plain old ex code. Go ahead, hit, enter, and what it's going to do for you is actually give you a quick set up run through which I want you to go through. And you'll be policed to know there is no installation off Swift three, so to speak, in X Code eight. And that's because X Konate uses Swift three by default. So once that's done, you're good to go with X Code eight and Swift. Three. There's one final step I want you to complete, and that is to go up to your ex code menu hit preferences and go to the accounts tab. This will allow you to sign in with your apple I d and your developer account. You can get a developer account for free, and you only have to pay once you start releasing APS. So it's a good idea to do this step now so you don't run into trouble later on. 3. How to Setup iOS 10 Simulators: the majority of the code that we write on this course is going to be run on a simulator or at least be able to be run on a simulator. Occasionally we're going to have code that we need to run on a device, for example, when we sort of want to use the camera on a device. Or we want to mess around with the battery on things like that but for the most part will stick with the simulator. Exco date should come with IOS 10 simulators installed, and you can check that by opening it up going toe window devices, and you get a whole list on the left hand side of what you have installed. Now, if you want to add a different simulator, click the plus icon at the bottom add simulator and it will give you a nice drop down off which kind of device you want to add, So iPhones, ipads, etcetera. You can also choose the IOS version, and now, if you want to download more, simply select that option and you have the whole back catalogue off IOS simulators. I believe with some effort, you can also get IOS seven simulators but at this point in time, it's not really worth coding for IOS seven, perhaps not even eight. Most devices run on nine, and by the time 10 is officially released, you'll have the rest of the devices running on 10. So that is basically how you set up your simulators. They should already be there. But if they're not hit the plus icon and Adam. 4. Setup a Playground in Swift 3: There's a great feature available inside of X Code, and it's called a playground, a playground. Let's you experiment with lots of code and swift or objective C, and it's a great way to understand what's going on with your swift code because you don't have the overhead of compiling the code. So it runs on a device or simulator With X Code open, you should be presented with a screen that gives you a few options. The first option is to get started with a playground. If you can't see that, simply go up to the menu file new and you can open up a playground from there, and this will ask you to name your playground and give it a platform. Obviously, this isn't IOS course, so let's give it on IOS platform and call it my playground. It doesn't really matter where you put it. Our playground comes furnished with two commands already, the first of which is this import You. I kit you like it, as you imagine, takes care off the user interface elements off our app, and it's a way for us to access the internal workings off all of the classes that swift provides us with. Then we have another line, which is simply the declaration off a string called Hello Playground. So this middle section is where your code sits. Over on the right hand side. There's a light, great bar, and if I just drag it out a little, you'll notice it has some text on there. It has hello playground, and it's simply repeating to us what our string is on the right hand side of that text. We haven't I so we can hit quick look, and it gives us a more complete picture of it in case that text was very long and we have a little plus icon, which is show result underneath, and we can get rid of that. So expand that right hand pain because it's going to come in very handy for the rest off this module. 5. Variables in Swift 3: variables in swift three or variables in any language. For that matter, variables are the fundamental building blocks off anything that you're going to create their little boxes. If you like that, hold bits off data or lots and lots off complex data. Let's see what they are by playing around with them inside of our playground. So in our playground, we've already got this import you like it and var str is equal to hello playground in quotes. Now what this is doing is its declaring a variable, and that variable is called str That name of the variable could be almost anything you like , But my recommendation to you is to name it something sensible. So when you look at your code later, Ron, it makes sense and you know what it does just from the name we have. This other word called var var implies this is variable i e. It might change at some point in the future. Then we have an assignment operator, which is equals. This takes the right hand side and puts it into the left hand side of the variable. So hello playground gets put into str. There are quotes there because This is a string and a string is essentially a sentence or a phrase or just a collection off characters like numbers and letters. Now what if I want to put something else inside? Str well I've already declared it and I've declared it by saying var str So if I want to put something else in it all I have to do now is reference it by typing str is equal to whatever I want to put in it. So now str holds value of Grant and it has dumped Hello playground It's important to know in programming there is no undue So if you do that, you cannot go back. Okay, so we have a bar. But what if I want to make that str unchangeable Let's say I'm working on a team of programmers and I don't want anyone to change the value of str I want them to get an error when they even try to do it well we can assign a let constant and a Let me just put that in str let me just delete all of this is equal to hello playground. What let's says is you cannot change this constant once you've assigned it the first time. That is why we're now getting a red error here. It tells us you cannot assign to value str because it's let its constant it is unchangeable . In reality, you're not going to use let that often But when you start working in teams, it is a very good idea to do that. So let me just remove this str and change this back too far. Str is equal to hello playground and the moment Swift is inferring the type. It knows that when we put things inside off these quotes that were actually declaring a string. But sometimes this won't be so obvious to swift. So we have to explicitly say and the way we explicitly say what type of variable it is is by popping in a colon and then typing the type. In this case, it is a string. Okay, now what if I want to store a number? Well, I would remove this line and I would put let's say number 18 and I'm going to get an error and it tells me that I cannot convert value off type into two specified type string and that just six is fancy words for Hey man, you can't put a number inside off a string, so we're going to change this type. Let's call this my numb to and interject. Andan Integer is simply a whole number like 89 10 12 and interject cannot hold any value that has a decimal like 1.1. If you try to do that, in most cases, at best, it will just drop the point part. At worst, you'll get some kind off crash. So how do you store decimals? Well, the one you should go for by default is called a double, and we can have double eight comma six or any number off characters after that. Or numbers. Now look at this. Very interesting. Over on the right hand side, it's told us that our value is this long one, even though we haven't typed that. And that's because Double has a fixed number off decimal points. Something like 15. I believe if I'm not wrong, so there are always going to be very small, very slight errors inside of programming. And if you're making something like a complex maths up, you need to be aware of that, right? What about other kinds of numbers. Well, we can have what's called a float. Onda float is like a double, but it has fewer accurate decimal points in accuracy on the difference between floats and doubles is you'd use afloat when accuracy isn't essential. There are some other reasons we choose between the two, but we won't go into at now. The final variable type I want to show you is called a Boolean. A Boolean holds true or false. So far, my bull, my book off type Boolean, is equal to true. Or this can be equal to false. So billions are incredibly useful for keeping track off. You know whether things are turned on or off or whether your user is touching the screen or not. All of these cases have true or false embedded into them, and you're going to use billions a lot in the logic off all your APs. So those are some of the basic types of variables inside of Swift three. I'm not going to run through all of them, obviously, because we'll be here for about five days. But once you know how to declare them and what they're used for and where to put them, then you should be good to go when it comes to learning more complex types 6. Collections Arrays in Swift 3: in Swift three. We have three different types off collections, the first of which are a raise, a razor useful for storing ordered sets off data. Apple's definition often array is an array. Stores values off the same type in an ordered list. The same value can appear in an array multiple times at different positions. So let's see what these arrays do by implementing some inside off our playground with your playground open, delete all of the code in there. Apart from the import statement, let's create first an empty array off inter jizz. Let's call this var. Some intense is equal to, and the way we define an array is by using square brackets. We tell Swift what kind of array what kind of variable type we want to store in this array , which is an integer in this case. And then we initialize it by using to empty brackets. So now we have an empty of ray off images. Let's create an empty array off strings. So far, some strings is equal to open your bracket, aunt, put the variable type insight and open and close your brackets to initialize it. Now, an empty array isn't exactly useful. So let's out of value to these. First. Let's take our some in Saray and then hit Don't and we want append and depend allows us to add a new element. So let's drop the number four into that of Ray. And of course, we can do the same with our some strings dot append. And I can put my initial into their or perhaps my two initials, G K. And if you'll notice on the right hand side, it tells us exactly what our arrays contain. G. K. And four. Now what happens if I try to put an integer in my strings? One. Let's say, six. We should get an error because we have said this array contains only strings, so you cannot put an integer into it. I could put a number in inverted comments as a string, but be careful because Swift doesn't understand that that's the number. It thinks it's a strength. So let me just revert these to how they were. Now there's a shorthand way off adding elements to your array, and we can have some intense we can have plus equals and then in brackets, we can add our ray and In fact, I could add a six and an eight to that array if I wanted. And Swift would quickly or swiftly add those elements into my some INTS array, as you can see on the right hand side. The same, of course, goes for some strings. Now, of course, we don't always have to do it this way. We can also initialize our array the following way. So if we come up to our top line, hit our colon, tell it this is an array of inter jizz, and then we delete this coat here. We can then give it an initial set off values for that array. So this declaration of the array is very similar to what we had when we looked at variables in Swift three. Of course, the same thing applies to string, so please try that. Now, if you want to also note that thes upend functions and these plus equal functions all work as before, it doesn't matter how much stuff is actually in your array, and I'm sure there's a theoretical limit. But in reality, you're never actually going to hit that limit unless you've program things really badly. Okay, what if We want to know how many elements are in our array. It's a very common thing that you need to know. For example, if you had an array of people's personal data like addresses, phone numbers and what have you, you would say, How many data entries do I have? So the way we get that is by having some it's dot count. And that returns a number off entries that we have four hours, some INTs and eventually when X code gets around to it. Come on X code, it should plussed out on the right. If it doesn't, we might need to add a print to that, and print will pop it out. So on the right, we now have seven elements in our some in Saray. There's a backslash end there because that stands for a new line, and that new line is translated down here in the console, where we can see a seven, and the next time we print something, it will put it below that seven. So don't worry about that. Back slash n. That's not part off your array or your counter anything. So of course we can do the same print. Some strings don't count, and that will also print the size of our some strings of Ray, which at this point is not very big. It's only one, and we can see that, plus it down here. And, of course, on the right and site. Now the final thing will look at is how to get a value out of the array. So let's say I want to get this number 88 out of some intense. Well, let's go ahead and print some ends. And then to grab an array, we have to refer to the index off it, which is something we put inside off square brackets. Now there's an important concept and programming in all programming. Is that a raise? Our zero indexed. So when we look at our some in Saray element number on the first position is actually indexed with the number of zero, then the one in the second position is element one in third position is element to. So we have 0123 zero index simply means we start counting from zero. And if you're starting out of programming, that is the most common mistake you're going to make. I promise you So we want to grab elements number three, which is actually at an index off to. And when we do that, it reports back that we have the number 88 sitting there. Okay? There are tons more operators, of course, for a raise, but those are the basics that will allow you to get going when making your APS. 7. Collections Sets in Swift 3: sets in swift three sets are the second element off collections, if you like after a raise. Now, a set stores distinct values off the same type in a collection with no defined ordering. You can use a set instead of an array when the order off items is not important, or when you need to ensure that an item only appears. Once, let's see what this jumble of words means by actually creating some sets inside of our playground. Let's create a set off letters. Let's call this var letters is equal to a set and then open our triangular brackets off characters. And this holds an alphanumeric character, and we can initialize it to empty with our two brackets. So now we have a set off characters. Let's insert a character with the dot insert function, which asks us for a new member. Don't worry what hash table means right now. Let's drop in the letter A. So in a way, it's kind of like a raise in the way that would define it, except we don't use upend. We use insert, and the reason behind that is the language, the intricacies of it. When we use upend it means we're adding something toothy end of something else. When we use insert, we're just putting it inside. We don't care about the order. So this is one of the great things about Swift. The language kind of goes after a linguist heart if you like, upend and insert do essentially the same thing, but with a slight difference that the language takes care off. So when you're unsure, look up the language in a dictionary and you should be able to figure out what it does. Now. Sets can be initialized just like a raise, so let's have a var off favorite foods. And let's make this a set equal to and let's have pizza. Yummy. Let's have wine and let's have wine number two And now we have a sent off our favorites. So obviously Swift is inferring the type of our set by looking at what it contains, which in this case, is strings. If we wished we could force it to contain only strings, what slight error that only strings. And that would also be okay, I would recommend to you generally toe always specify the types that you have. I might not do it because I'm an aged programmer and I'm falling into bad habits. But as a beginner, having it explicitly defined is going to help you out so much. Right now. Of course, we can use methods like Count on our sets to tell us how many elements of present, but we're not going to do that here because we've covered it in a race. But I do want to show you something here. Let's try to drop in another wine element inside off this set initialize er, and in an array that would be fine. We'd have four elements, but notice. In a sense, we now have wine only once. The second wine isn't actually added or it's added and overwrites the original. I'm not quite sure which. So if you initialize a set with two identical values, they will overwrite each other, and you will only have one only use sets. When a you don't care about the order and B, you have or want only unique values inside of the set itself 8. Collections Dictionaries in Swift 3: Let's look at the final collection in Swift three, and these are called dictionaries. A dictionary stores associations between keys off the same type and values off the same type, and it stores these in a collection with no defined order because you don't really need it . So mostly a dictionary in a programming language is much like a word dictionary, something you'd look up a key in, for example, a word. And you'd get the value from, for example, that word's definition. So let's look at how we implement these dictionaries inside off Swift three over an hour playground. Let's start with an empty document on just before we begin. I want to address this line at the top that says Playground now on etcetera, etcetera. This starts with two forward slashes, and that means it is a comment. Comments are not taken into account off by the compiler, so X code completely ignores them. There for us is humans to remember why we've just written that thing when we come back to it a few months later on, so back to dictionaries and a dictionary is something like this. Structurally speaking, a dictionary has a key, and then a value just like a word dictionary, as I've just sent. So let's create one of these. Let's have a var called names off in Tages, and this is going to be a dictionary that allows us to look up an integer like to and then returns the human readable name the word to. So let's initialize this with an integer as the key and then put a colon in and let's have a string as the value. Let's put our brackets in and initialize it as empty. Now I can give this its first key value pair, so let's have names off inter jizz and let's give it the key, which is in square brackets off to, is equal to the word to. So now our dictionary has one key value. Pair two and two. What if we want to put more than one string in there? Let's say this dictionary allowed us to look up the values off, too, in both English and Bulgarian. Well, that's simple enough. We can come up to this string declaration at the top, and we can make this an array so each integer will correspond to an array off strings. Now let's have names of vintages to equal to, and you'll notice we haven't error. And if we click it, it tells us we can't have a string, which is to, in this case, and assign it to an array off strings. So let's get rid of its complaints. Let's have two comma and let's put in another value. Bulgarian for two is so Now we have a dictionary that holds two and Davar. And how do we access these values? Well, let's have a print names off images. We open our square brackets much like an array and let's grab to. And then we need to put a question mark in. Don't worry about what this means for now, and then we have to pass it where or which value we want to get out off our string array. So if I want to grab Index one, that actually corresponds to Element number two and you'll notice on the side. It tells us we have divide optionals. I will cover later and back slash end. You already know what that means. So we have our value pulled out off the dictionary. Now, dictionary values are going to be very, very useful or the dictionary structure type later on. When you're creating APS, they're very, very flexible and a little hard to understand at first, but once you do, you will use them a lot. 9. Logic If Else and Switch Case in Swift 3: sometimes in our capsule programs, we need to test for certain conditions where we say, If something is this, then I want you to do this. Or if it's not that, then I don't want you to do this. And another way of writing that is, by using switch case statements, let's see what each of these are by implementing some of them inside of our playground. Let's start by creating a var. My NUM is equal to 33 which is my age. And then we're going to say if because we're going to test something related to my numb and we need to put that test inside off brackets and we say, If my numb is equals equals 33 now equals equals is called the comparison operator and it checks if mine, um, is equal to the interview off 33. Don't confuse it with a single equals, which is the assignment operator assignment operator, if you remember, makes 33 equal to my number. So we're comparing in this if statement, and if this is true than everything inside of the if statement is going to execute so open curly braces and swift should close on below for you. So if this is true, then I want you to print my number, and that should print it out. Over on the right hand side. There we have our print statement. So my number is definitely equal to 33. Therefore, it printed it. But what if we want to test for a different number? Well, we wouldn't want to write lots of if statements all chained up because there are lots of errors waiting to jump out at you with that approach. What we want to have here is an else statement. So we're saying, if my number is not equal to 33 then I would like you to check else if my number is greater than 33 and then I want you to do something. So let's print old man. So now if my numbers greater than 33 it should go to this section. So let's change it. Let's put it to 34. And look, we have changed our printed statement. We now have old man then what if we were comparing a number and we wanted to run a default statement? So if none of thes evaluated to true, we can have a default statement with else where we don't actually put anything in brackets . We just have a block code we want to execute. Let's print in this case young man whips. And now, if I change my numb to 31 for example, I am going to get young man printed out. I can even change it to minus 100 I guess. Technically, that is a really, really young man, and it prints out young man for us. So let me change this back to our 32. And now I want to show you the difference between an NFL statement on a switch case. And that is primarily the structure, because if else kind of gets convoluted and it's hard to read in a program, you know when you're skimming of your code, that would be difficult to understand immediately what's going on. So a switch case statement kind of gets around that limitation. Let's have a switch, and if you press the auto fill, it tells you switch requires a value, and then it gives you a case of pattern and code and a default off code, so we're going to pass over the value off my numb and we're saying of the case where my NUM is 33. I would like you to print my numb and we can add another case to that. If we wish. Let's have case and let's have from zero dot dot backwards triangle 33. So if we have a number between zero in 33 then I would like you to print Young and our default code. Let's have a print off, Old said Whoops. I've got to put that, of course, inside off quotes and then we should be good to go, right? So my current value of mine, um, is between zero and 33 which means my program prints out young if I change it to 33. Exactly all of my fl since switch cases change automatically. So if else and switch case allow you to check for a certain condition and if you have many off them, I would advise you to use the switch case statement method because it's just a little bit neater than a whole string off. If else's 10. Round and Round Loops in Swift 3: loops in Swift three. Occasionally in your programs, you need to perform some kind of action multiple times over multiple pieces of data. And rather than calling some function again and again and again, loops allow you to do that in just a few lines of code. So let's see how these loops work and all the different kinds that we have in Swift three by implementing them in our playground. The very first and most basic loop is available in most languages, and it is called the Four Loop. So let's write one. Let's start with four var I in zero. Don't don't less than 10 open curly braces and close them and inside of here, let's have print I. Now. What this loop is doing is it's creating a variable called I, and it's a signing the numbers zero etcetera To I, that's what for far, I in 0 to 10 means every time the loop goes round I zero, then I as one then I is to all the way until I is less than 10. So I is going to reach the number nine. It's going to go from 0 to 9, but never get to the number 10 because that's the condition we've specified. And we've said Each time you go round I would like you to print I out. And that's why over on the right, we see 10 times if I open up my eye. Icahn, we have the number nine, which is what I currently is. So this is the basic for loop. Now, if you've programmed a swift before, you might have done it a slightly different way. And this is the way that's recognized languages the world over where we would have a four var. I equals not we would have a semi colon until I is less than 10. And then we have an I plus plus, which adds one toe I and inside there would have a print statement or whatever we need it. But notice how swift three quickly throws an era and it tells us see style. Four. Statement has been removed in Swift three, and it suggests the version that we implemented just above it. So if you come across web tutorials and they recommend this for statement, it will not work. Let's delete it from our memories right now, right? So when would you use a four statement. Well, if you had an array of stuff and you wanted to go through each item and print it off or do some addition to it or whatever you want it, that's where four loops really come into their own. Let's have a far called numbers, which is going to be an Inter Jura ray. Let's have int is equal to a new Inter Jura ray. And let's just pop in whatever numbers we wish to put in there. Now let's create a four loop four variable n in numbers, open our braces and then let's print and out. So this four loop is going to go through each number one by one in that array, starting from Index zero so four and going through it element by element. So that's how you grab each element from an array. Then we have a different kind off loop, and this is called a wild loop. So let's have a far And actually, before I do this, I'm just going to delete this so it's clear for you. Let's have Our count is, is and interject, which is equal to note, and this variable is gonna keep count off our wild loop. Then we have a while. Count is less than 10. I would like you to perform the following actions, Prince. How account on my laptop is starting to stutter here. And I'm gonna tell you why. Look at this number on the side. This loop has gone round tens of thousands of times already. And you might say, Why is it doing that? Well, it's during that because this count is zero, which is always less than 10. And how do we get around that? Well, every time the while loop goes around, we have to add one to our count Count plus equals one. And look at my spinning beach ball of death on my Mac. It's not quite catching up with itself. So I'm just gonna pause the video here and come back when it's fixed. And I fixed it. So there, if you did the same as me, you've kind of had a mini Well, it's not a crash, but it's a place where your program gets stuck. And this area where it gets stuck is a problem that even experienced programmers like myself running to occasionally. So always check. There is an exit condition for your while loops or your four loops or whatever loops you have. Okay, there is a final version of the loop that is called a repeat, while loop where we ask it to repeat a certain function. So let's have print something like not yet and add one to our accounts, and then we give it a wild condition. So I want to repeat that function while count is less than 10. And if I just remove this while lips, so counter zero, it should go around the repeat loop 10 times and there we have it. So that's a repeat while. And the difference between a wild loop and a repeat while is that the repeat performs the function first, so the while Loop performs it as part off the loop itself. 11. Functions in Swift 3: functions in Swift three allow us to organize our code much more efficiently. A primary goal whenever you code in any language is to not write the same line of code more than once, and functions are a big part of helping us achieve that. Let's see how they work by implementing a function inside of our playground. Now. So far, I have this wild loop and account that goes round and round it. Now What if I wanted to write a while loop that did the same thing again? Well, I would have to copy all of that code, and that is a very bad thing. So let's create a function that uses that while loop and the way we create a function and Swift three is by using the keyword funk and let's call this check count and then we open and close some brackets. I'll come to those again in a minute, opening closed embraces. Now we're going to grab all of our count, and while code and dump it into that function and save it now, the first thing you're going to notice is that there is nothing printed out on the right hand side and That's because a function doesn't run as default. You actually have to do what's called calling the function, and the way we call it is by its name check count with two brackets, and now the function will get called Execute, and that loop should go round as many times as it needs to Now. The reason we have these two brackets here, the open and close is because of functions can pass over values or variables or other items to themselves. And what do I mean by this? Well, let's look at a basic idea of it. Let's go to our function of check count, and we're going to pass over a variable called message and this is off type string. So now available expects toe. Have a message string each time. And that's why we have this red era, it says, missing argument for parameter message in coal. And that's what these air cold they're called arguments or parameters, or there are one or two other ways of calling him. But an argument is a good word to remember, so we have to pass it over in check count. We have to say the message. I'm going to pass you is the following Not yet. And that string gets passed over to our check count and then inside of our Chek count, we can use that message, for example, here under print message and that will print out not yet 10 times so we can pass over these variables now. The advantage of doing this is I can now run, check count and I can send a message called Grunt and it's going to execute that code. And if I just drag up this bottom, look how it's executed, not yet. And Grant and the beauty of that is that Look, we've only rested two lines of code, and we've used the same function. But we've got in a different result. So that's the fantastic thing about functions. Now we can pass over more than one parameter. So inside off our function, let's have something we Passover called count zero. And of course, whoops. We don't want count zero. We won't count interject because this is of type interject. And now our to function calls are incorrect. Some skates delete this one because we don't need it. And inside of here I'm going to pass over my count and I'm going to give that zero. So now it's passing over two parameters to check count and inside off my check count function. I'm going to make this one equal to count. But this is kind of bad practice, because I have two counts here, so I would probably want to rename my count up here to something like P counts. This could be anything you like. I'm just calling it p count for past count. And of course, we have to change it down here to pee count. So always try to name your variables differently. Unless it makes sense to have, um, the same. So now if I changed this past over count to five, my loop will only run five times because the count is starting at zero. And this is a bit of a contrived example, but I think you get the idea now. The other thing is, always call your function something obvious. Check count. I've got a verbal, got a noun, and it's telling you exactly what it is doing. Always make sure it is descriptive. Don't make it too long. Make it short. But descriptive nonetheless. Now the final part off functions is the fact that they can return something, and the way we specify them, returning something is with a dash, a forward trying get a bracket, and we specify the type off thing it's going to return. It could be a string, an integer boolean, whatever you like. Now there's an air it down here and it says it's missing. Return in a function, and that's because it expects us to use the return word and then Pospech some kind off string. So let's have done as passed back. Now our check count receives back a string, and you can see that we have this done printed out with it. So it's a good way of passing back some kind of message from our strings or from our functions rather. 12. Optionals in Swift 3: optionals. Occasionally we need to have a variable that we don't want to write to immediately. We might write to it later on, though in most programming languages this causes to problems, the first of which is that if nothing is stored, then we get what's called a no reference, and this simply means that nothing is contained in the variable. This is normally when a crash occurs and this crash happens when your user is running the app, so it's not good. Secondly, we need to add a check for no, which means we need to write more code and write more carefully. Swift does away with this quite elegantly, but it is a little confusing at first. So let's get rid of that confusion by following along and looking at optionals inside off our playground. Now let's have a variable called my STR, which is of type string, but we're not going to give this of value right now. Instead, we're gonna put a question mark after it, and we are telling our compiler that this variable is optional. It might exist. It might not exist, and in that case it has two sets or values that it can have, one of which is nail, which means it has nothing. The other is a value, and that value contains the actual variable itself. So think off optionals as a special data type or it's not that special, to be honest. But I like to think of it is special that contains either nothing or something. And if it does contain something, we can get the value back. Let's try to print this. Let's have print my strength and notice what are compile. It says it's going to complain, it's running it and what's not going to complain in this case because we're only trying to print it and it tells us we have no inside of our string. Let's make it complain. Let's have a variable called New string, which is equal to my string. And then we're going to add on a suffix to that string so we can kind of slam two strings together, and now we have an error, and that is because value of optional type string is not unwrapped. Did you mean to use etcetera or etcetera? So now we've just got in a crash. We have an error, some fatal execution. There and that is our second crash. I believe in this part of learning swift, and that's because we don't actually have a value inside of our my string variable. And luckily for us, swift or ex code is telling us that they haven't Aaron now. And if you have this error, it won't let you compile the app and release it. It's impossible. Other languages might let you actually compile the app, which, as you can imagine, is a dangerous place to be in, because in Swift, we can see the error and fix it before people get ahold of the app. In other languages, we might not see it. Our people will get the app and it will crash. And to me, this is one of the primary reasons that IOS is ahead of Android in terms of app quality one because Apple review all the apse and two because they're advancing with these languages. Java is a lovely language, and I like using it. But it is easier to make mistakes in Java than it is in swift. The whole idea of Swift was to get rid off the silly little errors. So how do we check for these errors. Well, when we click this, it tells us we can insert an exclamation mark. And if we did that, what we're saying to Swift is that we are absolutely dead 100% sure that my str contains a string. It definitely does not contain nil. And you want to be careful with this because I'm going to insert that and now we have an era. So we make a mistake and a lot of Apple Swift. Developers call this the crash operator or something similar to that because it's a common mistake that lots of people make when they don't really know what optionals are, and they just use the compiler suggestions to fix it. So delete that exclamation mark and let's do this the correct way. What we want to do is have a statement that says, If let my str value is equal to my str and what that's doing, is it saying if you can get a value out of my string, then I would like you to do the following with that value so we can have my str value plus suffix so this will only run if we actually have a value in my str and you can see how we're avoiding those nasty no reference or nil reference air is now so I can now assign value to my string. My str is equal to Grant and that should now print out Grant Suffolk's inside of here because we actually have a value and that's pretty much all there is to optionals. We are occasionally going to see that exclamation operator and there are places where it makes sense to use it and it is okay to use it and I will highlight those as we go. You also, of course now know what question mark means And again, when we come across it, I'm going to explain what it means in terms off the item we will be working on at that point in time. 13. Classes and Objects in Swift 3: classes and objects are fundamental to all object oriented programming languages off which Swift is one. A class is simply a blueprint in code. It allows you to create multiple instances off itself, and these instances are called objects. So using one blueprint, you can create multiple items called objects. Let's see how this works in practice by following along inside off our playground. Let's create a class with the key word class. That's a word. You're going to see a lot, and this class is called fast. Come now. Unlike a function, there are no arguments or parameters to pass over in this part of the class. There are places we can do that, and I will cover that. But for now, we just want curly braces. Now a class is a blueprint. It's a container. It holds things like properties, variables, functions, structures in Yuma, rations, lots of other things. It can hold pretty much everything you can think of in programming. So the first thing we're gonna do is create this fast car class is give it a far called top speed, which is equal to, let's say, 155 And because it's fast car, we're going to say top speed of all fast cars is 155 which is the standard Elektronik Lee Limited Top speed. Then we can give this class a function and this function we can call yet top speed and this has no arguments. But it does return an integer, and this is going to return the top speed currently sitting in our class. And we could just right top speed. But a better way to write it is self and you'll notice. Self here refers to the class off fast car. So it's looking at itself and saying, OK, what stuff do I have? And if we put dot in one of the items that we have is top speed, so it's going to return its own top speed. Now let's come down out of the class and create a VAR called my car, and this is equal to fast. Come open and close your brackets. Now we have a car, said this car is using this blueprint of the class to create itself, and that means it has all of the properties and functions that are available inside off that class so we can have my car dot get top speed, and that should return 155 and there we have it on the right hand side. Now let's say our fast car has a different top speed each time we created, and we want to pass over the top speed well, rather than going through the Pfaff off changing that variable each time, we can use what's called an initial Isar in the class. And an initial izer is the method that gets called whenever the class is in Stan. She hated or it's used to make an object, and this normally is created just below our list off variables, and we're going to call this in it, and this is a special key word, and we're going to pass over a top speed, which is called, which is of type integer open and close our braces. Now let's change this top speed at the top to a type off interject. Then what we're going to do is we're going to assign the top speed. We pass over to the class variable. So we say self dot top speed is equal to top speed, and you'll notice how swift automatically knows that I am referring to self. So it's this one, and this top speed refers to this one. Even though they have the same name, they're actually different variables. So we have our top speed, and now we have an era when we initialize our car missing argument. So let's give it the argument at once, which is top speed. And let's give this one a top speed of 210. So now when I save it, I should print out a top speed off 210 over on the right. So whenever you get an error saying missing arguments, what it's asking for is the stuff in this innit function. So just to recap a class is a blueprint that allows you to create objects. And actually, before I go, let's have our Ferrari is equal to a fast car, and this has a top speed off. Let's say to 20 and in our Ferrari we can get our top speed. So look at how a class allows us to create multiple instances of itself on multiple different kinds. Off cars, classes and objects are one off the fundamental parts off object oriented programming, and you can't go wrong by knowing too much about them. They are that fundamental 14. Inheritance in Swift 3: inheritance in Swift. Previously, we've looked at classes and objects. Now can you imagine trying to write a class that covers cars, copays, trucks, family cars, etcetera? Well, there would be quite a lot of repeated code. And as off said before, we cannot repeat code. That's bad. So this is where inheritance comes in and saves us a lot of trouble. Let's see how inheritance works by implementing some of it in our playground. Let's create a class which is called Just Simply Car, and we're going to give this class only one property, which is let number of wheels equal to four because a lot of cars have four wheels. Then let's say we want a family. Clark. Well, let's have a class called family come now. I could initialize this and say the let's number off wheels equal to four. But hey, I've repeated code, and that's bad. Or at least I've told you that's bad. So delete that, and what you're going to do is put a colon in and say this inherits from car, so it grabs all off the properties, methods and things available in car and assigns them to family car than inside a family car . You can add various other properties that only sit with family car. So let's have let number off doors equal 25 Now if I created a family car far, my family car is equal to a new family car. We can now access both the number of wheels and the number off doors. So my family car dot number off Look, we have doors and wheels so I can grab the wheels and it tells me we have four. So our family car has inherited from the class off car. Now you can imagine this inheritance can actually keep going on and on. I could create a class called Big Family Car off, and this would inherit from just the plain old family car. So Big Family car gets all of these properties. But because family car inherits from car, Big family car also gets the number of wheels that are available inside off car and then inside of big family car. We can have of our size equals large if we so wish, so that is basically how inheritance works. And if I wanted to initialize a class, I could put my in its into class of car. So let's have an in it and num wheels, which is of type interject. And then we say, self dots. Numb wheels is equal to the number off wheels that we've passed over, some scant to remove that and make it an interview. Now we have an era on my FAM car because it wants the number off wheels and let's be hilarious and put six wheels on it now the number of wheels off our car. But we have a slight era, and I know what it is. We have to put our labelling num wheels is six, and now we have our line at the bottom. That tells us we have six wheels on our car. Ridiculous car. So that's how inheritance works. And again, it's a fundamental principle that we're going to see every single time we open up our IOS projects. 15. Summary of Swift 3: all right, You have covered a lot in this section on learning Basic Swift. Three. I'll just run through it quickly. But don't worry about remembering all of these. We're going to practice them as we go. You've learned about variables, loops, functions, classes and objects, arrays, sets and dictionaries, which are all collection types. The FL switch case statements, optionals and inheritance. There is a lot more to swift three, of course, but I don't want to bore you. I just want to give you the fundamentals that give you the basic tools you need to create APS. Once you know all of these concepts, understanding new things becomes easier and easier. Even though I've programmed for years and years, I still come across stuff that I've got a kind of cock my head at like a dog woodcock his head when he doesn't understand. And that's gonna happen your whole life. It never goes away, But rest assured, I have given you the fundamental tools you need to create whatever you can imagine.