Programming in Swift: Fundamentals

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

Part 2: Beginning Collections

12. Challenge: 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: 11. Tuples Next episode: 13. Arrays

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: 12. Challenge: 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.

All right, 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, but then keep watching to compare your work to mine. Good luck. (upbeat music) All right, challenge one, declare a constant tuple named specialDate that contains three Int values followed by a String. So use this to represent a date in the month, day, year format, followed by a word you might associate with that day. So I've picked the Worldwide Developers Conference, or WWDC as we call it, that Apple puts on every year in June. So in 2019, WWDC was on June the third. Okay, so first I'll declare the constant with let specialDate. Then in parentheses, I put three Ints, not specifying the type, but letting Swift infer the type, six to represent the month, June, three, the day, and 2019 for the year, and followed by a String, WWDC. Done, onto challenge two. Create another tuple, but this time name the constituent components. Give them names related to the data they contain, the month, the day, the year, and description. Okay, I'm going to declare this as let namedSpecialDate, and then in parentheses, I put the month, six, The day, three, the year, 2019, and the string description WWDC. Now, this tuple has exactly the same data as your first one, but in this format, it's much easier to reference. Onto challenge three. So in just one line, read the day and description values into two constants. You'll need to use the underscore to ignore the month and the year. Okay, so I'll start with let, and then some parentheses. So then an underscore to ignore the month. and a constant, I'll call it keynoteDay, and that'll capture the day, and then another underscore to ignore the year, and then another constant to capture the description. We'll call that keynoteDescription, and then I set that set of constants equal to the namedSpecialDay tuple. And that's done. Onto challenge number four. So 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, and then change the day to new value. Okay, instead of let, which you use for constants, you start 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 as I did for the namedSpecialDay constant. So the month, we'll default to nine, the day, we'll default to 10, the year, we'll default to 2019, and I'll default the name to iPhone Day. There, now to change something, all we have to do is put the name of the variable, iPhoneDay, and then a dot, and then the member of the tuple you want to change, so in this case, it's name, then the assignment operator, and put in quotes what we want to change it to. All right, it 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 learning about another collection, arrays.