7 days of WordPress plugins, themes & templates - for free! Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Basic Types

Once you know how to create variables and constants, you need to understand what types of data you can store within them. In this lesson I'll walk through the process of creating instances of the basic types that come natively with Swift: Int, UInt, Double, Float, Bool, and String.

2.2 Basic Types

Now that we can create variables and constants. We need to start to talk about the different types that are available to us within the Swift programming language. Now, there's a number of other types that are gonna be available to you in the different frameworks and the different libraries that you get access to once you start targeting a certain platform like Mac OSX or IOS or things like that. That are gonna be presented to you, and say like things like the foundation library, which we're not going to really concern ourselves so much with in this course. We're mostly gonna be talking about the different types that are actually built into the Swift programming languages that we're gonna have access too, all on their own. So the first thing that I wanna talk about are some of the types. So I can say, my integer and I want to maybe specify the type of this and I want this to be an integer. As you can see here we have a few different types. We have integer eight, 16, 32 and 64, which obviously are going to deal with the size or the length of the words within that integer. You can specifically choose one of these if you want your integer to be of a specific size. But the recommendation by Apple, by their documentation, and pretty much by me as well, unless you have a specific reason to use one of those. I would stick with the int which is kind of a specialized version, which is going to have its size dictated to you, depending on the hardware in the operating system that you're running on. So if you're running a 64 bit machine, this integer is going to be 64 bits long. And if if you're running on a 32 bit machine, this is going to be a 32 bit integer. Like I said before, you can absolutely use some of these other ones if it absolutely is necessary for you to do so. But if not then I'd definitely urge you to use the just plain out of the box. Now, at this point, I could assign this just like we saw before, I can say this is five. But maybe later on I wanted to say, well depending on the system I'm on right now what is the absolute maximum value that can be stored in an integer? Maybe the minimum value, how would I do something like that? Well, as I mentioned before, all of these types are more than just types. They're really just classes and they have different properties and functions, and fields assigned to them that we have access to. So I can simply use the int type here. And I can say give me the max. I have a property taxes that I can say give me the min I can access that. And there's a number of different functions that are on here that I can use to do all sorts of different operations on just these at the class level. And I get even more if I were to say, assigned five to this, and I can go into my integer, and I could do even more things like get the hash value like you saw before. I could do successors, I could go to the it max, I could get its value, I could do a number of different things as far as the integer is concerned. And there's a whole slew of other things that I can do on other types as well. Now let's just say that the integer is good because it'll span the negative numbers, as well as the positive numbers. But what if I didn't need to use the negative numbers? I only wanted to use the positive numbers. And in that case then, I would want to use an unsigned integer. And once again I get the same sizes that I had access to when I was working with an integer. I have eight, sixteen, thirty-two and sixty-four bit. And then I also have this one at the top here, that's going to size itself for me depending on the system that I'm running on. So once again, we're talking about 32 bit or 64 bit. And all the same operations work here. I could say, uit give me the, I could set this equal to uit.max, .min. And I can do all those same types of operations that I did before. .Now obviously the one thing I couldn't to is, if I were to assign this to five and then later on in my application some sort of operation, or me just not looking and paying attention. Assign this to be equal to two. This is not going to build obviously, because a negative number overflows when it's stored in an unsigned integer and you just can't do it. So this is another way for Swift and the compiler to tell you, hey you're not allowed to do that. Which is pretty nice because it's actually going to try to save you a little bit from yourself. So those are going to cover the basic integer styles of types that we're gonna work with. The Regular integer and the unsigned integer, but there's a couple different other numeric types that we're gonna work with, or that you're probably gonna need to work with at some point. And those have to do with what we're talking about floating point numbers or decimal numbers. So I could say my decimal. And at this point I have a couple options as well. I can work with a double, and I can also work with the flow. Now once again, the usage of these depends very much on what you're going to need to write, or need to use for these numbers that you're using within your application. And as you see here with float, I have a couple different sized options, once again 32 and 64 bit. But I also have the big one, so I can fit the 80 in there as well if I really needed to start working with some larger numbers with some very large calculations and precision. But once again the recommended value, unless something comes up, that you absolutely need though, is just going to be a regular float which will size itself as it needs to be. And then with double, you really just have double and that's pretty much all it is. Now you're going to get pretty good precision up to about 15 decimal places when it comes to double. And float does pretty well between say, you know, eight bit and all the way up through whatever the size is that you're working with now. If it's a coin flip between which one you want to use, maybe they will both work in a particular instance. Apple's documentation is going to urge you to use the double in that case. But ultimately at the end of the day. If you are just writing a simple application to work with a couple basic numbers, and the precision is not that important to you, then flip a coin and it'll work just fine. But if you really start to get closer down to the precision of the decimal places and how far out you get. Then you should really read up on the documentation to help you choose which one is gonna be best for you. Now outside of the numbers we have a couple different types as well. We have a boolean. Which if you've ever worked with any other programming languages before is going to give you access to set things to equal true or false. So this is going to come into play later on when we start to talk about control statements and being able to control the flow of your application and use loops. And if else statements and things like that, to really bring your application up a level to be able to handle some logic given some certain scenarios. But we'll definitely talk about that a little bit later. And then finally one of the other basic types that we are going to find ourselves working with quite a bit, is going to be the string. And I can once again set this to any sort of string that I would like. I could say Derek or Derek Jensen, or whatever as long as it's encapsulated or enclosed in these quotation marks. And then I can start to do different operations on the string as I would pretty much any other programming language, although there are definitely some gotchas here and there. Which we'll talk about an upcoming lesson during the demo. And I'll show you some nifty little ways to get around that. And then later on in the course when we start talking about extending the functionality of types using extensions. Then we're really going to show you some interesting things that you can do. But I'm just going to kind of introduce the basic concept to you now. Now if you ever done any sort of other programming. You've probably run into a scenario where you wanted to go in and maybe get a character of a string. So maybe I want to get the first character of this string. And I wanted to go into string and I wanted to do something like open and close square brackets. Because typically, in most languages, you can deal with a String kind of as an array. And I could specify give me some character at an index or something like that. Well, I would want to put in a zero in there, but as you're gonna see here this is not going to work, because Swift doesn't actually work this way. The subscripted is much more powerful than just being able to take an integer, but it's a little bit more of a pain. But I'm gonna show you an example of how we're gonna get around that in an upcoming lesson. So I'm going to going to introduce the problem here, and then we'll talk about it a little bit later. One other thing to kind of be wary of is that, when you're working with the different types within Swift. More specifically, the numeric types. If I had an integer, so I could say my int is gonna be equal to five and maybe I had a double. So I could say my double is gonna be equal to 5.0. So I can come down here and see my int sure enough is an int. my double yes is a double, and I want to say var sum is gonna be equal to my int plus my double. In a number of other programming languages something like this might work, but unfortunately within Swift it does not, because we cannot apply this binary operator between an int and a double. So if I really wanted to be able do something like this. I would have to kind of cast or coerce this double into an integer. So, in order to do that, I would have to use whatever type I was looking to do. In this case, I wanted to cast this to an int, so I can save this and build now, and then at this point I could maybe do something like print. And I can print my sum, and I could say that I could go ahead and run it. And as you'll see here in a moment I'm gonna get ten. So five plus five is ten, but just be wary of that, that when you start to deal with the different kind of classes or different types of numeric, say integer, double, and float. You're going to have to kind of do a little casting magic to get in between them. Just so that it'll work okay. Now let's jump into something a little bit more specific to Swift which, one of the things that I feel are probably one of the coolest features of the Swift programming language. Even though this concept is not new to Swift, it's in other languages but I do kind of like the way that they've handled it for a couple of different reasons. And that concept is known as an optional. So in the next lesson we're going to talk about optionals.

Back to the top