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
Since we need data for our app, let's create a model class. For this app our class is going to consist of an array containing separate instances of a playlist represented as a dictionary.
Teacher's Notes
Code Snippets
_library = @[@{kTitle: @"Rise and Shine",
kDescription: @"Get your morning going by singing along to these classic tracks as you hit the shower bright and early!",
kIcon: @"coffee.pdf",
kLargeIcon: @"coffee_large.pdf",
kBackgroundColor: @{@"red": @255.0, @"green": @204.0, @"blue": @51.0, @"alpha": @1.0},
kArtists: @[@"American Authors", @"Vacationer", @"Matt and Kim", @"MGMT", @"Echosmith", @"Tokyo Police Club", @"La Femme"]
},
@{kTitle: @"Runner's Rampage",
kDescription: @"Hit the track hard and get in beast mode with everything from dance tracks to classic hip hop. All the right fuel to motivate you to push your limits.",
kIcon: @"running.pdf",
kLargeIcon: @"running_large.pdf",
kBackgroundColor: @{@"red": @255.0, @"green": @102.0, @"blue": @51.0, @"alpha": @1.0},
kArtists: @[@"Avicii", @"Calvin Harris", @"Pitbull", @"Iggy Azalea", @"Rita Ora", @"Drake", @"Lil Wayne"]
},
@{kTitle: @"Joy Ride",
kDescription: @"Let this eclectic playlist take you wherever your heart desires. Cruise along in style to these energetic beats.",
kIcon: @"helmet.pdf",
kLargeIcon: @"helmet_large.pdf",
kBackgroundColor: @{@"red": @153.0, @"green": @102.0, @"blue": @204.0, @"alpha": @1.0},
kArtists: @[@"Afrojack", @"Kid Cudi", @"Daft Punk", @"kIcona Pop", @"Gesaffelstien", @"Roksnoix", @"deadmau5"]
},
@{kTitle: @"In The Zone",
kDescription: @"Keep calm and focus. Shut out the noise around you and grind away with some mind sharpening instrumental tunes.",
kIcon: @"laptop.pdf",
kLargeIcon: @"laptop_large.pdf",
kBackgroundColor: @{@"red": @51.0, @"green": @153.0, @"blue": @204.0, @"alpha": @1.0},
kArtists: @[@"Dr. Dre", @"Snoop Dogg", @"Com Truise", @"D12", @"Flying Lotus", @"Kanye West", @"Rundfunk"]
},
@{kTitle: @"Button Masher",
kDescription: @"Turn up the speakers and get out of my way! The ultimate gaming playlist to get you hyped up and ready for the crazy fun that’s about to happen.",
kIcon: @"joystick.pdf",
kLargeIcon: @"joystick_large.pdf",
kBackgroundColor: @{@"red": @51.0, @"green": @204.0, @"blue": @102.0, @"alpha": @1.0},
kArtists: @[@"Skrillex", @"The Game", @"2 Chainz", @"Jay Z", @"T.I.", @"Rihanna", @"Eminem"]
},
@{kTitle: @"Futbal Remix",
kDescription: @"There’s nothing like the world’s game. Kick around the field with this eclectic playlist from around the world. Futbal for life.",
kIcon: @"ball.pdf",
kLargeIcon: @"ball_large.pdf",
kBackgroundColor: @{@"red": @255.0, @"green": @102.0, @"blue": @153.0, @"alpha": @1.0},
kArtists: @[@"Shakira", @"Santana", @"Wyclef Jean", @"Aloe Blacc", @"Pitbull", @"Enrique Iglesias", @"Ricky Martin"]
}];
-
0:00
Let's start our data model, by adding a new class to our project.
-
0:05
So let's go to File > New, and then File again, and
-
0:10
under iOS source, select Cocoa Touch Class.
-
0:16
We're going to name this file MusicLibrary, so let's get rid of that.
-
0:22
We want this file to be a subclass of NSObject, and
-
0:26
make sure your language is objectiveC and then hit next.
-
0:30
Now, we want to add it to the algorithm group, in the algorithm project, so
-
0:35
hit Create.
-
0:37
Okay, so we're going to set this class up as our data model.
-
0:40
Now, there are a couple different ways we could structure our data model, but
-
0:44
we're going to keep it relatively simple.
-
0:47
Going to move these down.
-
0:51
Our data model is going to consist of an array, that represents our music library.
-
0:57
We're worked with arrays before, so this should be familiar territory to you.
-
1:02
Inside our array, we're going to store dictionary incidences.
-
1:07
Each dictionary will contain relevant information about each playlist,
-
1:12
that we can use to display on our playlist detail page.
-
1:16
If you are unfamiliar with the basics of arrays and
-
1:19
dictionaries, we have some great content.
-
1:22
Check out the objective C basics before proceeding.
-
1:26
Now we'll start by going to the header file, and
-
1:29
we'll create an array called library as a property.
-
1:34
We'll first start with the property declaration, specify strong nonatomic.
-
1:41
It's gonna be of type NSArray.
-
1:44
And we're going to call this library.
-
1:47
Now, to this array, we're going to append dictionaries.
-
1:51
Each dictionary will represent a playlist, and will have key value pairs for
-
1:56
things like the playlist name, description, artists included, and so on.
-
2:03
Now we'll do this using an initializer method.
-
2:06
Again if you're unfamiliar with the basics on init methods,
-
2:10
check out the link to the relevant video in a previous course.
-
2:14
So let's go over to the implementation file.
-
2:18
And in between, we're gonna start typing init.
-
2:23
And hit Auto complete to start the init method.
-
2:29
First, we need to initialize our super class, and assign it to self.
-
2:34
So, let's do self, equal super nit.
-
2:40
Now, let's check for self using an if statement.
-
2:45
If true, we set up our library array inside the if' block.
-
2:50
And then, after this if, we return self.
-
3:00
Each object in the array is going to be an instance of
-
3:02
NSDictionary containing the relevant key value page.
-
3:07
Now, there are a couple of different ways, we can instantiate our dictionaries.
-
3:12
We could use the NS dictionary class method, dictionary with objects for keys.
-
3:18
Let's give that a try.
-
3:19
Now, you don't need to type out what I'm about to do.
-
3:22
I'm just going to explain this to you, and then, I'll give you this block of code to
-
3:26
copy paste, since there's quite a bit of code, and it's mostly repetitive code.
-
3:31
First I'm going to make some space.
-
3:33
So that, we only have the editor on screen.
-
3:37
First, lets create an instance of a playlist dictionary using
-
3:42
the NSDictionary class method, dictionary with object for keys.
-
3:47
Now this method takes in two arrays.
-
3:50
An array of values, and an array of keys.
-
3:53
I'm going to use a keyboard shortcut to paste in some code.
-
3:59
Okay.
-
3:59
So we could create an instance of our playlist like this.
-
4:03
The first array using the dictionaryWithObjects for keys method,
-
4:08
takes in an array of objects, that is a list of values.
-
4:13
And then the second array is the list of keys.
-
4:17
Now we'll go over what these keys mean, in just a bit.
-
4:20
But, creating it this way this code is just for a single playlist.
-
4:26
We have six playlists, so we would need to have six of these.
-
4:31
With just one playlist, it's already relatively unreadable.
-
4:36
Then after we have all six of these,
-
4:38
we'd have to add this to the library array, which is our property.
-
4:42
So for the bottom here,
-
4:44
we can add it to the array using the NS array initializer method with objects.
-
4:50
And then we were passing each dictionary, as an object to this array.
-
4:55
Now this works, but there's a couple issues with our code.
-
5:00
First, it's going to be unreadable.
-
5:02
But more than that, we would be repeating code quite often.
-
5:06
If we replicated this six times,
-
5:09
we would calling NSDictionary, dictionaryWithObjects, forKeys six times.
-
5:14
And then repeating all this code over and over again.
-
5:17
Especially the keys.
-
5:18
We would be retyping the keys six times.
-
5:21
Now an easier way to do this, is using the NSDictionary literal syntax.
-
5:27
Now to create a dictionary using the literal syntax, go something like this.
-
5:32
Let's do NSDictionary riseAndShine, just like our last dictionary.
-
5:37
We start with an @ symbol,
-
5:41
followed by curly braces to create an empty dictionary.
-
5:45
We can then add key value pairs just like before.
-
5:49
So we could do someKey to someValue.
-
5:55
So this is a first step.
-
5:57
We could use this and get rid of the dictionary with objects, method call.
-
6:02
Gonna use another shortcut, to show you what that looks like.
-
6:05
And that would look like this.
-
6:08
Now we created the exact same dictionary object,
-
6:10
as we had before this time using literal syntax.
-
6:14
As you can see, it's a bit easier to read.
-
6:16
If you've noticed here in our artists key we have
-
6:20
a value we're passing in, an array.
-
6:23
Now we're creating an array using the literal syntax just like the dictionary.
-
6:28
So we could construct our library array, using this literal syntax as well.
-
6:33
We'd start with an at symbol, and
-
6:35
then instead of curly braces we use square brackets.
-
6:40
So to add this dictionary to our library array,
-
6:44
we can just wrap it in the array literal syntax.
-
6:47
For example, self.library equal rise and shine.
-
6:55
Now, there's only one problem though.
-
6:58
With six instances of playlist dictionaries,
-
7:02
we're repeating the same keys over and over again.
-
7:05
Even though this syntax is usually used.
-
7:07
I still have to write this out five more times, to create all my playlists.
-
7:11
By doing this we're not adhering to the DRY, that is,
-
7:15
don't repeat yourself principle.
-
7:17
We're writing code over and over again.
-
7:19
But on top of that, we're bound to make mistakes somewhere and
-
7:22
introduce errors in our code.
-
7:24
These keys are hardcoded strings.
-
7:26
And repeating them five times,
-
7:28
increases the chance that we're going to make an error somewhere.
-
7:32
What we can do is create constants, or
-
7:34
constant strings, to represent each dictionary key, and
-
7:38
then use those variables as keys instead of retyping the string every single time.
-
7:45
This is a common pattern in iOS development.
-
7:48
Let's start by defining our constants at the top,
-
7:51
under the import statement at the top of our file.
-
7:55
Gonna make some room.
-
7:56
We start with the class.
-
7:58
So, NSString, cuz we're creating a string, then the asterisk.
-
8:03
But instead of naming the string as we usually do,
-
8:07
we're going to add the keyword const.
-
8:10
This creates a constant pointer to a string.
-
8:13
When creating constants in objective C, it's standard practice to start the name
-
8:18
of a constant with a lower case k followed by the name of the constant.
-
8:23
So in our case, let's create a constant for the first key title.
-
8:27
We'll do kTitle, and then we'll assign it this string,
-
8:32
gonna copy paste it, put some space.
-
8:37
There we go, title.
-
8:38
Now, in our dictionary code, instead of using this string title.
-
8:43
We can just use the variable.
-
8:46
Similarly, let's go ahead and create constants for all our keys.
-
8:49
[BLANK_AUDIO]
-
9:23
Okay, so now we can replace all the hard coded strings,
-
9:27
using our constants that we just created.
-
9:30
But, rather than having you watch me retype all these keys, and
-
9:34
then add five other playlist dictionaries,
-
9:37
I'm just going to get rid of this code and copy paste that in.
-
9:41
So now we have self.library, which is
-
9:46
assigned an array, in using the little syntax.
-
9:50
Inside that array, we using the literal syntax again,
-
9:54
we create dictionaries containing playlist instead, with this
-
9:58
dictionaries we're using a constant string, as the key for key value pick.
-
10:04
Now, the structure of our dictionary is simple enough.
-
10:07
We have a key for example title along with the value as a string.
-
10:11
Now all values are stored as strings, except for artists, which is an array
-
10:17
containing names of the artists included in the playlist, stored as strings.
-
10:22
Also the background color.
-
10:24
This contains a second dictionary,
-
10:26
as you can see here by the dictionary literal syntax.
-
10:30
We're storing the red, green, blue, and alpha values here, so
-
10:34
that we can construct a background color for our cover images.
-
10:38
Even our Icon keys return a string with the name of the image that we're going to
-
10:43
use as our playlist cover.
-
10:45
The Icon strings will match up to the images we loaded,
-
10:48
in the image assets folder earlier.
-
10:51
That is how a single playlist dictionary is structured.
-
10:55
Since we have six playlists,
-
10:56
we had six dictionaries in our array, to represent our entire music library.
-
11:02
Now as in a site, if we were building a real music library app
-
11:07
we would not be hard coding data like this.
-
11:10
In reality, we would be pulling that information off of
-
11:13
a server on the Internet and creating our data models as we get that information.
-
11:19
That's a bit more advanced, though, and to start off, we're just going to
-
11:22
hard code our data, so we don't have to worry about all those details.
-
11:26
Now, just to reiterate, we created these constants for a couple different reasons.
-
11:31
First we're using variables here, so you see that I don't have to
-
11:34
retype the hard coded string every single time for all six dictionary instances.
-
11:40
But even more important, down the road let's say I wanted to change this title.
-
11:46
Let's say I want to change this key in my dictionary.
-
11:50
If I had hardcoded strings I would have to go in everywhere,
-
11:53
wherever the key is used in my map and change that string every single time.
-
11:58
Now, now that we have constants I can just come up here, and
-
12:01
change this title to something else say title,
-
12:04
and that's going to be reflected in all the dictionaries in my library instance.
-
12:09
That makes a lot more sense, and
-
12:11
we're being better coders by adhering to the dry principle.
-
12:15
Let's change this back.
-
12:19
Creating the library was our first step.
-
12:21
Every time we want information about a playlist, we don't want to
-
12:25
have to deal with the entire library array and it's dictionaries.
-
12:28
Why?
-
12:29
Because it would be fairly cumbersome.
-
12:31
We'd have to iterate over the array grab each dictionary,
-
12:35
know the correct keys to get the values we want and so on.
-
12:39
To make the data easier to work with we're going to create a second class called
-
12:43
playlist, that will pull the information we need from the library and
-
12:47
package it up for us to use.
You need to sign up for Treehouse in order to download course files.
Sign up