Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Tuples

They're very useful, but some languages omit them as the first class collection types—tuples don't always get the respect they deserve. Swift, though, has strong built-in support for tuples. So I've devoted an entire lesson to just tuples! It's not because I feel bad for tuples, it's because so many other capabilities of Swift depend on them. Let's check tuples out now.

3.3 Tuples

The last basic way to group some data together that I wanna talk to you about before we move on, is that of a tuple. And the reason I'm calling it just a way to group some data together and I'm not really calling it a type, is because it's not really a type. And the way that the compiler handles it behind the scenes, it could probably be considered a struct. But really at the end of the day, for our purposes, we're just gonna use it to group data together. Now I said this is called a tuple. And you may hear myself included and other people call it a tuple from time to time, but either way is just fine. So really all a tuple is is nothing more than a group of pieces of data that are housed within parentheses that are comma separated. Now they're not like other collection types. Where we're gonna be dealing with a lot of common data types, or common pieces of information that are of the same data type. Like all integers or all strings or all doubles. We're really gonna be talking about any data type of any value. And we can just group them all together so we can keep them combined and pass them around. And we're gonna see a lot of usefulness in this in several places that in, when we start talking about functions and when we start talking about pattern matching and a number of the different conditional statements. And managing the flow of your application but for now we're just gonna understand the basics of the tuple itself. So the best way to describe it is to give a very simple example. So let's say that in a very simplistic world I wanted to describe a particular employee at a company. And so, I could use that I could use a very complicated class or a structure, or things like that if I really wanted to, but I wanna really keep this simple. So I wanna just say, let an employee be equal to a tuple of data. And the tuple is going to include at least for this example two pieces of data. It could include many more, as many as you really want, but for our case we're going to keep it relatively simple. Now those two pieces of information that it's going to contain are going to be the employee ID and the name. So let's say the employee ID is 12345, which is is obviously an integer. And then the second value is going to be in my name, Derek Jensen. So I can split this up into two fields and have the second field be Derek and the third be Jensen. But for simplicity's sake we're just going to leave it like this. So we have this tuple now and we can now pass this around. We can pass it as input arguments and output values from functions. And we can do all sorts of wonderful things with them but ultimately at the end of the day. I need to know how to access it. So I know how to create one now. Now how do I get data out of it? So there's a couple of different ways to do that. Some a little bit cleaner than others, but the by default way you can actually get data out of this is by referring to the values within this tuple by their index. So we can talk about the zero index being the employee ID. And the one index being the name so I could do something like, let ID equal two. And, the way that we refer to these just from creating a vanilla tuple like this is by using the name, dot and then the index of the value that we want to get to. And, as you see here, we're going to get this IntelliSense that's gonna say you can either have Index zero or you can have Index one. The first one being an integer and the second one being a string. So this is a pretty nice piece of functionality that comes for you in Swift that it looks into that tuple. And it knows what it is and it can determine, it can infer the types of all of the different pieces of data in there. So I wanna get the ID out so I can use. Employee dot zero. And then I could say let name equal to employee dot one. And then I could do something simple like print employee ID is named name. And then I could go ahead and just put in my string interpolation here. So I can concatenate these strings by name, like that. So we could go ahead and save it and run it. And once we run this, we should see that employee 12345 is named Derek Jensen. And that's great, so that's gonna work out very well. But the problem here, and you are probably seeing this as well, is how we are referring to these values within here. We can do this, but ultimately it's pretty ugly. And I really wouldn't want to have to remember which index is what value and all that sort of thing. So we can do it this way but its kind of ugly, I don't really like it. So what's another way that we can do this? So I'm going to comma these. Fields out here. And then let's figure out a different way. So another way that we could do this, is we could actually, when we refer to this value later on. We can create a mapping using somewhat of a version of pattern matching, which we're gonna continually talk about in bits and pieces as we go along in this course. But ultimately what we can do, is we can say I want to let, and I can create a tuple here, or a pattern that we're going to match to. And we're gonna set that equal to our employee. Now what I can do within this pattern is I can specify names that I want to refer to the values found within this tuple that are stored within employee or EMP. And now, when we do this we have to make sure we are specifying names for each of the values in there so we can't specify too many. So this way I could say, let id comma name be equal to emp. And by doing this, you'll see that my little error went away and I could now save this and run it. And I should get the same result. Now, one thing to note here as we're gonna see periodically throughout this course as well. When we're doing this kind of pattern matching, what we ultimately can do is we can say, you know what, I only need one of those pieces of data, I don't need both. And how do we do that? And we're gonna see this periodically as that I can specify a wild card for one of these to say, you know what? I know there's a value there, but I don't really care what it is because I'm not gonna use it later on. So let's say I only cared about the name, and I didn't care about the ID. I could specify the wild card value, which is actually the underscore. And then I would obviously not have access to this ID anymore, so I could just say, employee is named, name. And I can go ahead and run that so it'll change the output a little bit because we won't get the idea anymore, but it will still work. We can see the employee is named, Derrick Jensen. So that's a little bit better I think. It's not really ideal still, because now I'm having to create a tuple. And then I'm having to do this kind of matching, creating another tuple to refer to the tuple values and it's getting a little bit ugly again. So the final way that you can ultimately refer to these things. And the one that I happen to like the most Is going to be by specifying named arguments when we're specifying this tuple. So now what I can do instead of just leaving this as this vanilla tuple. I can actually specify names to refer to these, or named arguments to refer to these values as. So the first one I can give a name, and I'm gonna give it a name of ID. And I'll specify a colon, and there we go. Now we're going to refer to the ID as 1, 2, 3, 4, 5 and then the second one is going to be named with a colon, and now I'll be able to refer to Derek Jensen as name. So now when I come into my print statement, I can now refer to employee Dot id and employee dot name. So now I'm gonna have access to these names to refer to the pieces of data that are found within there. So I can go ahead and run this, and I will once again get the employee one, two, three, four, five, his name Derick Jensen. So those are the basics of the tuple. Now this may seem fairly simplistic, but you're gonna find later on as we start to get into some more advanced topics. This is gonna become very handy and you're gonna be able to use this in a number of different places to do some pretty cool things. But it's very important to understand the basics, if for no other reason than you're gonna start seeing these things around quite a bit.

Back to the top