Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 2: Beginning Collections

11. Tuples

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Introduction Next episode: 12. Challenge: Tuples

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 11. Tuples

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Now so far, you've been working on single pieces of data, like one integer or one string, but sometimes data in your apps should be in pairs or triplets or even larger groups because the data has meaning when it's all together. Now, a great example of pieces of data that have meaning when they're put together is latitude and longitude, which you can use to find a specific location on a map or on your GPS. In Swift, you can represent related pieces of data like this with a data type called a tuple. Now, you may hear pronounced like "toopal" or "tupple" but no matter how you hear it pronounced, they're all the same thing. Open up a playground provided with this part of the course, find the zero two tuples page and start there. Let's create the first tuple in your playground. You'll keep extending the student and mark examples that you worked with in the previous section of the course. Now you define a tuple just like you would a constant. You start with "let" and then the name for your tuple. (keyboard clicking) And then you have to declare the types that'll be contained in your tuple. Now, the nice thing about tuples is that you can mix and match types as you need. In your previous examples, you have the student name stored as a string, and their mark stored as an integer. So you simply use parentheses to surround the list of types in your tuple. In this case, a string and integer. (keyboard clicking) And then you can tell Swift what actual values should be stored in your tuple. (keyboard clicking) Now you have a constant that has not one, but two pieces of information in there. The student name and the student mark. Now, how do you get this information back out of the tuple? Items in a tuple can be referenced by their position in the list and their numbers starting from zero. In this case, you have the string in position zero and the integer in position one. (keyboard clicking) To get each of these values separately, you can just use the name of the tuple, followed by a dot, followed by the position or index of the value you want. So, to get the name of the student, you can say, "student mark, dot zero". (keyboard clicking) And to get the student's mark, you can say, "student mark dot one". (keyboard clicking) Now it's not really convenient to use ones and zeros to reference the individual members of your tuple. Wouldn't it be nicer to use actual words instead of numbers? Well, you can. Let's create a tuple that has three members, a name, a mark, and a pet. First, let student data, and then set that to, in parentheses, name, Chris, mark, 49, and pet name, Mango. (keyboard clicking) You'll notice that I didn't specify the type of each member. That's because Swift uses type interpolation to figure out the correct type for each member in here. So now that I've named members of my tuple, it becomes a lot easier to reference each of them from code. So instead of the index of where a member lives in the tuple, you can simply use the name of the member. So I can get the name of the student with student data dot name. (keyboard clicking) I can also get the mark of the student. (keyboard clicking) I can even get the pet's name. (keyboard clicking) So let's handy for sure, but it can be a little tedious to pull each of those values out on separate lines, but you can use a shortcut trick to get those values into separate variables, all in one line of code. So you can do the following, let, and then in parentheses, declare your variables or constants, and then set that equal to the tuple name. (keyboard clicking) So in just one line of code, this creates three constants. Name, mark, and pet, and sets each of them to the value of the member in the same position inside the tuple. So "name" gets the value at index zero. "Mark" gets the value at index one and so on. Let's take a look at it there. (keyboard clicking) Now there's one last trick to show you. Say you only want to pull some of the values in a large tuple. In this case, we don't care what mark Chris has, but we really do care about his pet. Now you can still use the shortcut syntax here, but we're going to replace the name for the element you don't want with an underscore. So in this case, we don't care about the mark. So we say, let, parentheses, name underscore, pet, takes a value of student data. And then we see that this only declares two constants, name and pet, and ignores that middle value. And that's it. That's the basics of using tuples. Tuples are great if you want to easily group a set of values together, and later in this course, you learn another way to combine values using structs and classes, but tuples are still really useful when you need something to put some values together in a quick and temporary way.