(2020) Swift Tutorial for Beginners: Lesson 16 Arrays

Sharing buttons:

hello and welcome to my Swift tutorial

series for beginners today you're going

to learn how to manage a collection of

data in what's called an array if you're

working with many pieces of data it's

going to be hard to manage them all

using just constants and variables so

let's take a look at how arrays can make

our life easier

alright stay tuned so Swift has a couple

of collection types that you can use to

manage your data easily and the one

we're going to talk about right now is

called an array and this is a collection

type that is ordered by indexes and that

means that there is a specific order to

this list of values so you can see here

in this demonstration or this example it

starts at 0 and then it goes 1 2 3 4 and

so on and so forth and at each position

there is a value or a piece of data so

there is a defined order and that's

really important to know let's jump into

the playground where I can demonstrate a

need and an example of how to use a race

so right here I've got three variables a

B and C and let's say that I wanted to

change these strings to my dog my cat

and my bird well I can go about it this

way and say something like this I can

just add the word my with a space to a

alright so a is dog I'm gonna add my in

front of it and reassign it to 8 so that

would be my dog so I could repeat this

with B and C but I mean this is only an

example with 3 items let's say we had a

hundred or 200 items then that would be

very very tedious right what I could do

is also I can put these values into an

array and what that looks like is it

starts with a pair of square brackets

and each item in the array is separated

by a comma so I would have dog comma cat

comma and then bird so there's no comma

at the end it's only in between the

actual items so this is an array

of three items with dog starting at

index zero cat at index one and bird at

index two so just like what we saw here


dog cat and bird that is our rate so

having this array is great but we need

some sort of way to reference the items

in the array so why don't we assign this

array to a constant called my array and

in order to access these items all I

have to do is say my array and then use

my square brackets and then put in the

index of the value that I want so if I

want a dog I would put in index 0 so

it's print that and we can see dog if I

wanted bird I would put in index 2 right

because it starts at 0 alright so in

that case I could do something like this

my array 0 is equal to my space my array

0 kind of mirrors this statement right

and this shows you that you can use this

sort of same syntax and there's actually

an error here this is a really great

example because I'm trying to assign

something into my array at index 0 but

this is a constant so I can modify it

after it's a sign so I actually need to

do variable like that let's make my

array a variable all right so this is

great because it shows you that this

syntax is to retrieve the item at index

0 but I can also assign things into

index 0 using this assignment operator

like that right so the resulting value

at index 0 is guess that my dog right

but this isn't very efficient either I

mean I'd have to write this line three

times or however many times for you know

however many pets I have

so this is where it gets really powerful

because you can use loops we learned way

back then in conjunction with a race so

let me show you how that works so for

example let's use a for loop or a for in

loop I mean let's say there's a counter

in 0 to 2 right so now I can say I can

well why don't we just print it out for

now so we can print out my array and

then it's access the index I can put in

counter in there so let's see what that

looks like if I run this code see I'll

print out my dog cat and bird but

actually it's because this line modified

what was at index 0 so let me get rid of

that line and let me print this again

so you can see dog cat and bird it's

merely printing out each index of that

array but already this is really

powerful right because we have one line

of code and it's being looped three

times but each time it loops it's doing

something different what if we did

something like this what if we did my

array counter is equal to my right space

plus my array counter alright now we're

talking because what we're doing here is

each iteration of the loop we are

accessing that index and then we are

adding my in front of it and then we're

reassigning it to that same index

basically changing the value and then

we're printing it out here so if I had

200 items in my array I would just

change this to 199 right and that's very

little code compared to what we were

doing up here right so you can start to

see how powerful it is to use arrays

with loops now what if we didn't know

the range of

the array right right here I'm assuming

that I know there are three items in

here that's why I'm doing this range

zero to two well the array comes with

some handy properties that we can use

one of them is called count which

returns how many items are in the array

it always starts at zero so we're gonna

start at zero if we want to access the

first item in the array however we are

actually we can use the count property

of the right so we can do my array dot

count but they're gonna run into a

problem with this let me show you why so

if I run this code you're gonna get my

dog my cat my bird and then index out of

range whenever you see this error index

out of range you start to think this

error message means that we're trying to

access an index that doesn't exist in

that array it's out of the range of the

array well why is that because my array

dot count returns the number of items in

there ray right so it returns three so

our loop actually goes from zero to

three inclusive so it's gonna start at

zero it's gonna do 1 and it's gonna do

two and then it's going to do three and

there is no fourth item in our right

right this is 0 this is 1 this is 2 and

when it tries to access index 3 there's

nothing and that's why it crashes and

gives us this error index out of range

so what we actually have to do when

we're working with a race like this and

we want to basically use this count in a

loop we have to do this minus 1 and then

that's going to go from 0 to 2

essentially now it's really good that

you know about the count property now of

the array because that's really useful

but there's actually an even easier way

to write this for loop we can do

something like this for each item in my

array do something with it

that's really the easiest that you know

the easiest that you can get so you can

do you know if you need the index for

some reason and you need to do something

with it you need to reassign something

then you'd have to use this method where

you're accessing where you're getting

the index but if you just want to let's

say out put each item or you want to you

know use each item without reassigning

it into the array then this sort of

format is great what it's going to do is

it's basically going to loop through

every single item in my array and put it

into this variable for you to use so let

me comment this part out actually let me

let me just comment this whole thing out

and just a quick tip if you want to

comment out a whole block of code you

can press a command back slash and that

just takes care of that so when we run

our code here so you can see this loop

very simply it's just printing out the

items of the array so this is a quick

and easy way to loop through items in

the array all right some other cool

things I want to show you how to declare

an empty array I think this is really

important to know because right here we

are initializing this variable to this

array but what really is the datatype

for this array if you were to explicitly

write it well I'll tell you it is an

array so there's square brackets and

inside you put the data type of the

array so string this array can only

contain string objects all right forgot

to mention that part that your items in

the array will be the same data type if

you wanted to declare an array that is

empty to start you could do something

like this let's say empty array


so you could just have an empty array

like that but you have to declare the

datatype because there is no datatype

inside this empty array for Xcode to

determine what data type that array

should be that's why we have to

explicitly specify that right here or if

you don't do this you can do something

like this let's do MTR a2 is equal to

you can do that as well so this is

basically creating a new array object

right this is a type it's a string array

and you're basically creating a new

object of that string array type and

it's not going to contain anything by

default all right now let me show you

some ways to work with arrays in terms

of adding items and removing items so

you can let's to add items first so you

can do my array there's methods like

append so you're gonna pend a new

element or you can even insert so you

can insert a new element at a particular

index and it's not going to overwrite it

it's just gonna push everything back so

let's say I wanted to insert frog at

index 0 then if I let's say I copy this

down here and I print out the items

again you're gonna see that it starts

with frog this time and then it goes dog

cat bird so it inserted for all I got 0

and pushed everything else back now

another way you can add things you can

go plus equals just like that and then

you can add a number of items to the

back of the array so I can do frog bear

and why don't we move this print


down here and let's see what is in our

array at this point so we have frog dog

cat bird frog and bear oh and that was

that's because I inserted frog at the

beginning and then I appended frog and

bear at the back so that's our resulting

array all right so let me just write it

here in case you download the playground

it's another animal that I can use

there's so many animals out there

raccoon it's not like I saw a raccoon

out there but it just popped into my


all right remove items so my array you

can remove a particular item at a

specific index like that it's gonna

remove that item and everything's gonna

shift to fill its its place we can

remove all right we can move first and

remove last we could move the first

number of items so there are a lot of

options here like I said these

collection types are meant to make your

life easier to manage collections of

data and that's why there are so many

handy methods for you to use another

thing that you might find yourself

wanting to do is searching for a

specific item in your array or maybe

finding out if an item exists in your

array or not search your array and the

method to use for that is there are a

couple of options there used to be one

called index of so you can a returns the

index of the item that you search for if

it doesn't exist in your array it would

return negative one otherwise it will

return the actual index button now there

are methods like first index of which

returns the first index where the

specified value appears because

theoretically like we saw before frog

existed at index zero and existed at

index maybe six or something like that

in our array and then alternatively


is also last index of so you're

searching from the back towards the

front and it's going to return the last

index of that item what you're looking

for so those are a couple of handy ways

for you to do some searching within your

erase what I have covered in this lesson

is enough for you to get the main

benefits out of using erase as we

continue to build apps together we're

going to learn new ways of using a race

but for now this is more than enough if

you liked this video please give it a

thumbs up and please consider

subscribing if you haven't already now

click on over there for the next lesson

and I'll see you there