How to Code in Swift 3 ✅ | Grant Klimaytys | Skillshare

How to Code in Swift 3 ✅

Grant Klimaytys, Software Engineer

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

      2:30
    • 2. Setup a Playground in Swift 3

      2:18
    • 3. Variables in Swift 3

      7:17

About This Class

This class takes you gently through from NO programming experience and teaches you Swift 3 - Apples latest and greatest programming language.

Essential if you want to make iPhone or iPad apps!

Transcripts

1. 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. 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. 3. 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