Programming in Swift: Fundamentals

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

Part 2: Beginning Collections

15. Challenge: Arrays

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: 14. Operating on Arrays Next episode: 16. Conclusion

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: 15. Challenge: Arrays

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, welcome to your next challenge. Now you've learned quite a lot about arrays and have time to use that knowledge now in this array of challenges. You can find the challenge in the challenge arrays page of a playground you've been using so far, or you can download a new one from the resources for this video, open it up, give the solutions to try on your own, but then you can keep watching to compare what you've done with what I've done. Good luck. (upbeat music) So challenge one, using the players array below and the array methods and properties learned about in this video, determine the following things about this array. A, the count of elements in the array, B whether or not the array contains the string value Charles. C, the first element in the array and D the last element in the array. So, first I need the count. I can simply ask for players dot count. (keyboard typing) And X code tells me that there are five players in the array. Next I can ask, does players dot contains and in parentheses Charles as a string (keyboard typing) And no poor Charles is not yet in the game. So similarly, you can see who the first person in the array is since Alice is the first person declared in the array. She is the answer to players dot first. (keyboard typing) And because Frank was the last person declared in the array, he comes up as a result of players dot last. (keyboard typing) Onto challenge two. So some new players have joined the game, Charles, Gloria, and Hermione. You'd like to add them to the array, Gloria and Hermione at the end and Charles, somewhere in the middle. So A, insert Charles at index two in the array B, add Gloria and Hermione at the end of the array at the single line of code. All right, the more the merrier. Now, you can't just use the append method here. Since you want Charles to go in a specific spot in the array. Instead, you can say players dot insert, and in parentheses, Charles as a string, and then at colon and two, (keyboard typing) (heavy breathing) And you can see that Charles is right where he belongs at position 0 1 2. Great. (heavy breathing) Now you can use a form of append to add Gloria and Hermione to the end of the array. Instead of calling append twice, as you might have thought to do, you can just make a short array of two people and append that short array to the end of a players array. So start with players and use the add assignment operator that's plus equals, then square brackets to indicate to swift that I'm creating a little temporary array. And then just put the two strings in there that I want to append in this case, Gloria and Hermione. (keyboard typing) So I can then print out the array to see what's inside. (keyboard typing) And there we go. Everyone's in their places. Onto challenge three, create another new constant array named team one that consists of the last four members of the players array. And that would be the range of elements from four through seven. Okay. This one is a little tricky, but not too hard. First declare a constant with let team one (keyboard typing) Then equals two-sided a value. And then you use the word array and capitalization matters, followed by parentheses to tell swift you're creating a new array. Now you can tell swift what to put in the new array by putting that inside the parentheses. Now, in this case, you want to use the last half of the player's array, which you do by saying players. (keyboard typing) And then in square brackets, the range of the indexes you want to take from a players array, which is four dot,dot,dot then seven, which means four to seven inclusive. And if I print that out.. (keyboard typing) There we can see that Team one is composed of the last four elements of the players array for Eli Frank, Gloria, and Hermione. Nice. So that's the end of your challenges as you grow in your iOS development career, you'll find lots of ways that arrays are used as handy little containers to organize your information. So head on to the next video in the series where I'll recap what you've learned and get you ready for the next section of this course.