There we go.
So this is a pretty big convenience initializer, and
what it does is it takes an NSData object that contains that JSON text,
and it also takes the source URL of where this feed came from,
and then it starts out here with an array of feed items, and it's a blank array.
That's a mutable array that we're going to put all those feed items into.
And then, we're also going to fix that data, so
this is just a little detail here that because of the Flickr data, there's
some weird characters in it that the NSJSON serialization class doesn't like.
So I just created this little simple fix.JsonData function that's going to
remove some of those funny little characters and in that way we'll be
successful in translating our JsonData into swift objects.
So the first swift object that we're going to back from the JSON
serialization initializer is the dictionary.
And it's going to be a dictionary of strings and
any objects because our JSON dictionaries can maybe hold either arrays or
it can hold Other dictionaries or strings or numbers so what we're going to get
back is initially just going to be a dictionary of strings and any objects.
So we initialize this JSON object by trying to load
the data using the JSONObjectWithData method.
And that takes our repaired data and we're going to
cast it into that type that we're expecting for the JSON object variable.
If it doesn't exceed it will likely return nil.
And it will likely raise an exception here.
We're just ignoring that exception for now and if we do get nil,
if we can't cast this JSON object into a non-optional variable feed route, we're
actually just going to return nil and this feed will not be initialized properly.
Okay, so this JSON object is a dictionary.
And actually, sorry, we're going to use feedRoot now.
So this feedRoot is a dictionary and it can be indexed by strings.
So each of those strings that are act as the keys for the dictionary actually
map now to the strings that are in the top level dictionary of the JSON object.
So, if you want to look at the title of the entire feed, you would look for
the title key in that dictionary.
We want to pass by the title of the feed itself and look for
the individual items in the feed, so we're going to look for
that items string, and under that it's actually an array.
So we're expecting an array of items.
So if I go back here and look at the array, we'll see that
we're hoping to get this items and cast it to an array of any objects.
If it doesn't succeed, if it comes back as something we're not expecting, again,
we're going return nil.
So this is an advantage of Swift's static typing,
is that it's easy to make sure that you're getting the objects that you're
expecting before it becomes too late.
Okay, and then after that, what do you do with arrays?
Well, you generally iterate through them.
So we're going to iterate through each item in the items array.
And then each item itself is going to be a dictionary,
just like our top level object is a dictionary.
Again, we're going to look for another dictionary, but
each of these items is going to be an individual photo with several properties.
To find the image URL, we have to look through the media and
the n keys, so if I go here and look through here,
there's a media key, and under that, there's an n key.
And then under that, there's an actual string that is that URL.