The course is part of this learning path
This course focuses on advanced techniques in Swift. You'll be introduced to structs and how we can use them in our code.
Hi, with in this lecture we're going to learn a new concept called tuples. So, tuples provide us tools to store collection of data. So, it's kind of array but it has a different structure and so far we haven't been using tuples, but this is an advanced fifth section so you have to know how tuples work. So, if you work with them in the future, or if you encounter one of the tuple returning functions, then you will know what to do. Let me comment out this print so that we can continue within the same main.swift and we will have our own tuple lecture. So, let me show you how to create a tuple and how to store data in it. So, it's very simple actually. You can just say myTuple and you open one parenthesis and it's a regular parenthesis, it's not an array parenthesis or it's not a dictionary parenthesis. It's a regular parenthesis and you can just write whatever value you want inside of it, like an integer or a string or a bullion or even an array. So, it goes like this: open the parentheses and say 1,3 for example, you put comma to differentiate between these values. So, this is the first element and this is the second element. And how does it differ from the array? So, when you create a tuple, you may want to get one of these elements by just going to another line and say myTuple, and as you can see it's an integer, integer tuple right now because we defined it so, and if you say dot, you can just choose the zero in order to get the first element. So, let me print this out and you will see, we will get the one integer like this. If you say myTuple.1 of course you will get 3. So, this is how tuples work. If you want to save a couple of coordinates for example, or if you want to save another couple then tuples might be for you. Of course we can do it like this. If you want to have a triple number you can just define it and you can just call zero, one or two. So, for example if I call two it will just give Me five. So, this has the same index logic of the array. So, can I do that? Can I change the tuple value? So, let me try to print out myTuple.2 and you will see we cannot do that. In order to do this, we have to define myTuple as a variable rather than constant. So, this is one of the difference between tuples and arrays. So, you can just create a variable tuple, but if you don't want to change the elements later on, you can create it as a constant. So, let's see some other examples of tuples to understand them better. For example, let me create a tuple3. So, it will have a string and the number inside of it. Like, let myTuple3 will be my name like Atil and we can have a number inside of myTuple as well, and if you write myTuple3 you will see that this is a string to integer tuple. Can I just have strings or integers? Of course not, I can't create a tuple with an array for example. Let me try to create it, like, I can come over here and say 10, but then I can open an array and I can say 10 20 30. For example in myTuple4, how can I reach the number 20? So, try to post the video and try to print it on your own. So, it goes like this, myTuple4, if you say dot and if you say one it will give you the array of integers, this one. And if you open a parenthesis and give an index here like zero, it will give you 10, and if you give the index one it will give you 20. So, that's how you Reach the number 20. So, can I pretty find the values that the tuple is going to get? Of course I can, like I can pre define a variable to be a string like this. So, we can say let myString is going to be an optional string and this will take only the value of a string, not an integer. So, I can do that for tuple. So, I can say let predefinedTuple is going to be a string, two string tuple. So, that I can just come over here and say predefinedTuple.0 is Atil and predefinedTuple.1 is Sam, for example. And if I run this, nothing will happen because I didn't even print it out. So let me say 'print predefinedTuple' and it will print out this string to string tuple, as you can see now we have the Atil Sam in a tuple. So, can I predefined the keys of the values? I can do that as well. For example, let me create a new tuple, and in here I'm going to say name and this will be a James and the metallica member bullion, this will be true, for example. And now, instead of saying tuple.1 or tuple.0, I can just say print newTuple.metallica or print newTuple.name. So, this is another difference between arrays and tuples, I can just predefine the keys of the values inside of a tuple as well. So, this is cool, I can just come over here and say newTuple.name, newTuple.Metallica. So, this is how you work with tuples, and in swift you won't come across tuple such often, but in another programming languages like in python, you will just see them a lot. And if you come across a library or a module that returns tuple, then you know how to do it, you know how to work with them right now. And of course you can create your own tuples and you can effectively store a couple of data or a tuple of data inside of a tuple so that you can reach them later on. So, let's stop here, and within the next lecture, we're going to see a new concept called Guard Let.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.