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.

Transcript: 15. Challenge: Arrays

Hey! Welcome to your next challenge! You’ve learned quite a lot about arrays, and it’s time to use that knowledge in this array of challenges!

You can find the challenge in the “Challenge - Arrays” 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!

Using the players array below, and the array methods and data you learned about in this chapter, determine the following things about the 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 (d) The last element in the array

players.count
players.contains("Charles")
players.first
players.last

So, first, I need the count. I can simply ask for players dot count. And xcode tells me that there are five players in the array.

Next, I can ask does players dot contains, in parentheses, charles, as a string. And no, poor charles is not yet in the game.

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.

And, because Frank was the last person declared in the array, he comes up as the result of players dot last.

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.

(a) Insert Charles at index 2 in the array. (b) Add Gloria and Hermione at the end of the array in a single line of code.

Hey! The more the merrier. Charles gets to join the team!

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.insert, parentheses, Charles as a string, and then at, colon, and 2).

You can click the reveal icon in the sidebar to view the resulting array; and you can see that charles is right where he belongs, at position zero, one, two. Great.

Now, you can use a form of append to add gloria and hermione to the end of the array. Instead of calling append twice, you can just make a short array of two people and append that short array to the end of the players array.

players += ["Gloria", "Hermione"]

So start with players, and use the add assignment operator, 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.

Create another new constant array named teamOne that consists of the last four members of the players array; that would be the range of elements from 4…7

Okay, this one is a little tricky but not too hard.

First, declare a constant with let teamOne. Then equals, to assign it a value, and then you use the word Array (capitalization matters!) followed by parentheses to tell Swift you’re creating a new Array.

Now, you can tell Swift what to put in the array, by putting it inside the parentheses.

In this case, you want to use the last half of the players array, which you do by saying players, and then in square brackets, the range of the indexes you want to take from the players array, which is 4, dot dot dot, 7, which means 4 to 7 inclusive.

And Xcode tells you that team one is composed of eli, frank, gloria, and hermione. Nice!

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 information.

Head on in to the next video in this series where I’ll recap what you’ve learned and get you ready for the next section of this course!