Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Arrays are fundamental structures in programming and have countless uses. We'll learn what they are, what their limitations are and how we can create them. We'll also review the concept of indexes, elements and mutability.
-
0:00
In this video, we will learn about arrays, specifically will learn about the NS array
-
0:04
and NS mutable array classes we find in Objective C.
-
0:08
So what is an array generally speaking?
-
0:11
Well it's an ordered collection of items where all of the items
-
0:14
are some type of object.
-
0:16
You could have an array of strings or
-
0:18
an array of dictionaries or even an array of arrays.
-
0:21
You could also have an array that mixes different types of objects.
-
0:25
Those items in the array are known as its elements.
-
0:28
As you can imagine, arrays come in handy for
-
0:30
storing all sorts of things like the names of every student in a classroom or
-
0:34
the results of some query from a database.
-
0:37
So I mentioned that in Objective C, we have two flavors of arrays an NS Array and
-
0:42
NS Mutable Array.
-
0:43
As you probably guessed, NSMutableArray's can be changed whereas NSArray's cannot.
-
0:48
Also in case you're interested, NSMutableArray is a subclass of NSArray,
-
0:52
so it inherits all of its characteristics except where they've
-
0:55
been intentionally changed.
-
0:57
You already know for
-
0:58
example that NSMutableArray has the added capabilities to have its elements changed.
-
1:02
That is to say, replaced, removed, or resorted.
-
1:06
But for now let's focus on NSArray since it's a bit simpler and
-
1:10
learning about it will also help teach us about its subclass NS mutable array.
-
1:15
So here we see NSArray of names.
-
1:17
We created it using the literal syntax.
-
1:20
That's the at sign and the brackets book ending our array.
-
1:24
Now let's notice a few things about NSArray.
-
1:27
Firstly, every element in it is the same type, N-S string.
-
1:31
By the way, those NSstrings are declared with the literal notation too.
-
1:34
That's the @ in quotation marks for those keeping score at home.
-
1:39
One other important thing to remember with N S arrays,
-
1:41
is that there elements must be objects.
-
1:43
By that I mean, they can't be primitive or
-
1:45
scalar types like ints, floats, bools or doubles.
-
1:49
If you want to store that type of primitive data,
-
1:52
you need to wrap them in NS numbers or some other Objective-C object type.
-
1:56
It's also worth noting that elements in an array can be repeated.
-
1:59
For example, in our array Kenneth appears twice.
-
2:03
If you were interested in creating a collection where you can't repeat an item,
-
2:07
you'd probably wanna use an NSSet rather than an NSArray, but
-
2:11
that's a horse of a different color.
-
2:14
Now some of you may have noticed that we created this NSArray object,
-
2:17
without using Aleken in it, which we just spent an entire video on.
-
2:21
Well that's because we use the literal notation, so
-
2:23
you don't need to explicitly call Alec in it.
-
2:26
However, you could, and then your code would look like this.
-
2:31
You'll probably see that alloc]init syntax a lot in older legacy code, which may have
-
2:35
been written before literals were used for arrays in Objective C.
-
2:39
You could also create that same array like No explicit Allegan in it,
-
2:44
but it also doesn't make use of literals.
-
2:47
In both of those,
-
2:47
it's worth noting that you need to include nill as the last item in the list.
-
2:52
That tells the compiler that the array has no more elements to add.
-
2:55
So I can get an accurate count for the array.
-
2:58
Before we move on it's worth pointing out that you can't create NSMutableArrays
-
3:02
using literal syntax for mutable or
-
3:05
changeable arrays, you'll need to use options like this.
-
3:08
And this.
-
3:11
And this.
-
3:13
Literals just won't work.
-
3:16
So now that we've created our shiny new NSMutableArray like,
-
3:20
[SOUND] what are we going to do with it?
-
3:23
Well, we can do lots of things to it.
-
3:25
We might iterate through it, replace one of the elements, or
-
3:27
simply retrieve one of those elements.
-
3:30
To do this, we need the index of the element or it's position in the array.
-
3:34
That's pretty simple to do, but
-
3:35
there is one important wrinkle you need to remember when we index arrays
-
3:39
the first element isn't at index one, but actually index zero.
-
3:44
So for our little array element at index zero is Andrew at index one you'd
-
3:50
find Kenneth to retrieve the element in the third spot you could simply type, and
-
3:55
string *name equals [ nameArray objectAtIndex; 2 ];.
-
4:01
That's the third element but the index is two since we started counting it zero.
-
4:06
Doing that retrieval will work for initializers as well,
-
4:09
since we aren't actually changing the contents of the array itself.
-
4:13
One of their common task is getting the count of elements in the array.
-
4:17
That's pretty easy to do, you could simply type, int arrayCount=[nameArray count];.
-
4:24
Now with mutable arrays,you will probably want to do more than just get the count,
-
4:29
let's go over some of the most common actions,
-
4:32
if we wanted to replace the first item with the name Gabe, we could simply type
-
4:37
name array replaceObjectAtIndex:0 with Object:@"Gabe".
-
4:41
To remove it completely, we'd simply type name array removeObjectAtIndex:0.
-
4:48
To insert an additional element at the third spot and
-
4:52
bump all the following elements by one index, we could use this syntax,
-
4:57
NameArray3, insertObject newName atIndex:2.
-
5:01
To add a name at the end of the array regardless of the current number of
-
5:05
elements, it's as simple as nameArray3 addObject zed.
-
5:10
The compiler will automatically add it to the end,
-
5:13
you don't need to explicitly say what the length is.
-
5:17
I hope you found that introduction to arrays helpful.
-
5:19
Don't worry if you don't remember all the syntax,
-
5:21
you can always go back and look it up.
-
5:23
It's much more important that you start getting a handle on what they are,
-
5:26
what they're used for, and what you can do with them.
-
5:29
Speaking of which, let's reinforce some of those concepts with a quick quiz, and
-
5:33
then move on to some hands on practice in Xcode.
You need to sign up for Treehouse in order to download course files.
Sign up