Notes: 12. Challenge: Tuples
Update Notes: The student materials have been reviewed and are updated as of October 2021.
It’s time for your next challenge!
You can find the challenge in the “Challenge - Tuples” page of the playground you’ve been using, or you can download a new one from the resources for this video. Open it up, and try solving the challenge questions on your own, then keep watching to compare your work to mine. Good luck!
Declare a constant tuple named specialDate that contains three Int values followed by a String. Use this to represent a date (month, day, year) followed by a word you might associate with that day.
I’ve picked the Worldwide Developers’ Conference, or WWDC, that Apple puts on every year in June. In 2019, it was on June 3.
Okay - so first you declare the constant with let specialDate.
let specialDate = (6, 3, 2019, "WWDC")
Then, in parentheses, I put three ints, not specifying the type but letting Swift infer the type: 6, to represent the month, June, the sixth month; 3, the day, and 2019, the year, followed by a string , “WWDC”. Done!
Create another tuple, but this time name the constituent components. Give them names related to the data that they contain: month, day, year and description.
Okay! I’m going to declare this as let namedSpecialDate. In parentheses, I put the month, 6, the day, 3, the year, 2019, and the string description, wwdc.
let namedSpecialDate = (month: 6, day: 3, year: 2019, name: "WWDC")
This tuple has exactly the same data, but in this format, it’s much easier to reference.
In one line, read the day and description values into two constants. You’ll need to use the underscore to ignore the month and year.
Okay, I start with let, then parentheses, then an underscore to ignore the month, and a constant keynoteDay to capture the day, then another underscore to ignore the month, and another constant keynoteDescription to capture the description. Then, I set that set of constants equal to the namedSpecialDate tuple. Done!
let (_, keynoteDay, _, keynoteDescription) = namedSpecialDate
Up until now, you’ve only seen constant tuples. But you can create variable tuples, too. Create one more tuple, like in the exercises above, but this time use var instead of let. Now change the day to a new value.
Okay! Instead of let, which you use for constants, you start of with var, since you’re defining a variable tuple.
I’ll call it iphoneDay, since this is the day we hear about new iphones, and I’ll do pretty much the same thing I did above for the namedspecialdate constant; the month will default to 9, the day will default to 10, the year will default to 2019, and I’ll default the name to iphone day. Nice!
var iPhoneDay = (month: 9, day: 10, year: 2019, name: "iPhone Day")
Now, to change something, all you have to do is put the name of the variable, iphoneday, then dot, then the member of the tuple you want to change; in this case, it’s name. Then, a single equals to assign a new value to the name member, and put in quotes, “Time to buy a new iphone”.
iPhoneDay.name = "Time to buy a new iPhone"
Looks like I’d better help you finish up this section of the course so I can get in line for that latest and shiniest iphone!
Now that you’re done with the challenges, you can take some of the concepts you learned with tuples, and take them into the next section, where you’ll learn about another collection, arrays!