Learn Swift for Beginners 2026 - Lesson 12 - Arrays
8:39

Learn Swift for Beginners 2026 - Lesson 12 - Arrays

CodeWithChris 05.05.2026 352 просмотров 24 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
Learn what arrays are and how to use them in Swift! 🧑‍💻 Resources: 📚 Full guide: https://codewithchris.com/learn-swift?utm_source=youtube&utm_medium=video&utm_campaign=learn_swift_lesson_12 In this lesson, you'll get a clear introduction to Swift arrays — one of the most essential data structures in programming. We'll cover everything from creating arrays, accessing values by index, understanding zero-based indexing, and working with data types. Timestamps: 0:00 Introduction & lesson overview 0:45 Creating variables — the problem with many values 1:11 Introducing arrays — multiple values in one variable 1:51 Accessing array values by index 2:21 Zero-based indexing explained 2:59 "Index out of range" error explained 3:30 Arrays are like lists — adding & removing items 3:49 Data types and type inference with arrays 4:10 Array data type syntax ([String], [Int]) 4:34 Array of integers example 4:58 When you must specify the data type (empty arrays) 5:22 Why you'd create an empty array 5:40 Alternative syntax for empty arrays 5:52 Another way to declare an empty integer array 6:19 Variables vs. constants with arrays 7:33 Printing first, last, and count 8:32 Wrapping up What you'll learn: - What arrays are and why they're useful - How to create an array in Swift - Accessing elements using index numbers - Zero-based indexing (and avoiding "index out of range" errors) - Array data types ([String], [Int]) - Creating empty arrays and when to specify the type - Variables vs. constants with arrays 📚 This is part of the Learn Swift series - a beginner-friendly course teaching you iOS app development from the ground up using Swift and Xcode. https://www.youtube.com/playlist?list=PLMRqhzcHGw1bVghYq_TAiPeoKaBVlOJu5 👇 Drop your favorite movies in the comments! #Swift #LearnSwift #iOSDevelopment

Оглавление (17 сегментов)

Introduction & lesson overview

Hi, welcome back. Let's continue. We are talking about arrays in this lesson. Now, I know in the previous lesson we were on chapter 3, we're doing four loops with ranges, but in order to continue with four loops over arrays, you first need to know what arrays are. So, we're actually going to take a little bit of a detour and we're going to come back to this lesson after you learn about what arrays are. In fact, we have to jump down to the first lesson of chapter 5. And then we can hop back to the chapter 3. So, first of all, what are arrays and how do we create them? Let me walk you through that in playground. So, you learned before that we can create a variable and then we can assign it a value such as Tom. If I had another

Creating variables — the problem with many values

name, I could declare another variable and have another name. But, if I had five different names, 10 different names, creating a variable for each name would be very cumbersome. That's where arrays come in handy. So, an array allows you to put multiple values in a single variable. Let's see how we do that with this.

Introducing arrays — multiple values in one variable

Instead of declaring two variables, name and name two, I could declare a variable called names and I can assign it multiple values. So, here autocomplete actually does that for me. But, in order to specify multiple values into one variable, we use these square brackets or angle brackets. And then we're going to put each of the values separated by a comma. So, I'm going to put Tom, comma Chris. And this together this is called an array. Now, you might

Accessing array values by index

wonder, how do we get the values out because this one is very obvious and simple. I can just print out name and it's going to print out Tom. Or I can print out name two and it's going to print out Chris. What do we do here? Well, this is how you would print out Tom using this array here. You specify the name of the array and then you specify angle brackets and you specify the index of the value that you want.

Zero-based indexing explained

want. Remember, we separated them by comma, right? So, the first item in array is always zero. These arrays are zero-index based. That means that the first item is at index number zero. So, you can take a look. It's going to print out Tom. Now, if I print out the value at index number one, that would be Chris. And this is a very common mistake that beginners make because when they see two items, they think that printing out Tom is one and Chris would be two because it's the second item. But, in fact, you're going

"Index out of range" error explained

to get an error, index out of range. So, whenever you see this error, index out of range, you know it has to do with perhaps you're trying to get a value in an array where there is no index. The other thing with arrays is that you can get the count. That means the number of items in that array. Like this. names. count will give you two because there are two items.

Arrays are like lists — adding & removing items

Now, the cool thing with arrays is that you can add items to it and you can remove items to it. It's almost like a list. Uh we're going to get to how to do that later on, but in this lesson, I just want to introduce you to arrays, tell you what they are, why they're helpful. Okay, some other things to talk about.

Data types and type inference with arrays

So, remember here in terms of data types, um because we haven't specified the data type of this variable, Swift will take a look at the value that you're assigning into that variable and it will infer that name is a string data type variable.

Array data type syntax ([String], [Int])

variable. So, and same thing goes for name number two. This is a string type variable. Now, an array also has a data type. Would this data type be a string? Close. It's an array of strings. That is the data type. So, this is how that looks. Square brackets string. And this is the data type for an array of strings.

Array of integers example

If it were an array of integers, this would be int. So, that would look like this. var nums Something like that, right? Again, we don't have to explicitly state the data type because Swift can take a look at what we're assigning to it and infer that. Now, where specifying the

When you must specify the data type (empty arrays)

data type is required is if you want to create an empty array because when you like this, Swift has no idea what data is in there. It's empty. So, it doesn't know what data type this is. So, in this case, you have to specify the data type like that. Why would you ever want to create an empty array? Remember, an array, you can

Why you'd create an empty array

add items into it and remove items from it. So, you might want to declare an empty numbers array only later on to add or subtract to or from it. So, that's basically arrays in a nutshell. Let's take a look at what else we have here.

Alternative syntax for empty arrays

So, we talked about the type of an array. This is another way to create an empty array. It gets a little bit confusing when there are multiple ways to do things.

Another way to declare an empty integer array

So, let's say we wanted to declare another empty integer array. This is another way to write it. Use the one that feels the most natural to you so that it comes easily. I mean, now we're just getting into different ways of doing things, but the basics of arrays is what we talked about earlier on here. Let's take a look at the Oh, this is worth mentioning. So, you learned about

Variables vs. constants with arrays

variables and constants and how if you declare it as a constant, you can't change the value of it, right? Same thing goes for arrays. If it's a variable, you can add or remove items, but if it's a constant, then it cannot be changed. So, that makes sense, right? Okay, let's take a look at this challenge here. Pause the video if you want to try this out for yourself. Create an array called favorite films that holds the titles of three films you enjoy and then print the first film, the last film, and the total count of films in the array. All right, so let's try this out for ourselves. Challenge. So, we're going to say let movies equals We're going to declare a string array. There's going to be three items, right? So, I'm just going to create three strings like that. I'm going to fill in the names. So, I really liked These are my favorite movies. Interstellar, really like that. I have a lot of favorite movies. These are some of the older ones like Green Mile. And I really liked Memento.

Printing first, last, and count

And we're going to print out the first one. So, print out movies zero. And that's going to print out Interstellar. Then we're going to print out movies The last one would be two, right? Because there are three items. So, this is index zero, this is index one, this is index two. And then print out the count. So, hopefully this was pretty straightforward for you. And if you haven't seen any of these three movies, I do highly recommend them. They're some of my favorites. Let me know what your favorite movies are in the comments down below. All right, let's move on to the AI prompts. So, this gives you an alternate explanation of Swift arrays. This gives you a quiz and lastly, these prompts give you some more examples to help you really solidify arrays. Okay, I hope this was helpful. In the next

Wrapping up

lesson, we're going to jump back to how to repeat things with Swift. All right, I'll see you in the next one.

Другие видео автора — CodeWithChris

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник