Programming in Swift: Fundamentals

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

Part 3: Control Flow

23. Challenge: Iterating Collections

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: 22. Iterating Collections Next episode: 24. Nested Loops & Early Exit

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: 23. Challenge: Iterating Collections

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.

Hey. Okay. It's time for your next challenge. Now you can find the challenge in the "07: Challenge - Iterating Collections" page of a playground you've been using, or like always, you can download a fresh one from the resources for this video. So open it up and try solving the questions on your own first, but then as always, you can keep watching to compare your solutions to mine. Good luck. (funky music plays) Okay. Challenge number one, the pastries are back. So I've provided an array of pastries below. I want you to create a loop that iterates over each element of the array and uses an if statement inside the loop to print out the pastries that start with a letter C. [computer mouse scrolling] Okay. So let me set up my for loop to iterate over all items in the array. [keyboard typing] Then I need an if block and I can use this condition. If the element of pastry at pastry dot start index, which is the first character in the string is equal to C. [keyboard typing] And if so, then I want to print out the pastry name. [keyboard typing] I'll execute my playground. And then console shows me that C is for cookie and cupcake and cruller. Good work. Challenge two. Now, writing tidy and compact code is the mark of a good developer. So let's try and tighten up the code from challenge one. So I'll begin by creating the for loop again. [keyboard typing] And then I'll add a where clause, where the first character of the pastry name is equal to C. [keyboard typing] So that filters the range of elements in the array to just the ones that begin with C. That means all I have to do now is print out the names of the pastries that I'm iterating over. [keyboard typing] And that's it. There's the same list of pastries in the console. So you can see how your code is a little easier to read now with fewer lines. Head on into the next video. And I'm gonna go over nesting loops and how to exit early. I mean, an early exit from loops, not an early exit from the video. There's still a few videos left to go. I'll see you there.