Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Swift
  • Overview
  • Transcript

3.2 Introducing Our Parser

When creating an app such as this, a large part of the functionality deals with retrieving data from the internet and parsing it into some useful data structure; namely our Models. This lesson will focus on how we are going to accomplish those tasks. Luckily for us, the iOS SDK has our back.

3.2 Introducing Our Parser

Now that we have a few model classes created, we can start to work towards getting some data into those models. So over the next couple of lessons, we're gonna start to actually retrieve some data, download some data, and parse some data, and ultimately get it into these models so we can present it to the user. Via our views. If we're just being honest here, if you've ever worked with the NVC pattern before, you'll know that I'm gonna to throw this out there as a, as a generality, as a generalization, so take it for what it's worth. But typically, the models and the views within NVC are typically rather. Boring for lack of a better word. Most of the time you are going to see a lot of the work done within the controllers whose job is going to be to pass data and interaction back and forth between the models and the views. And then maybe go out to web services or websites are where ever it may be to go and get some data. Now there's other situations where you might have the models do. Some of that works, or maybe have some of the business logic, maybe, if you're working with applications that are handling maybe bank accounts, or banking institutions and things like that. You might, in certain instances, you're gonna find more logic, kind of living in that world of the model. But in our case, it's gonna be rather boring, and the controller is where most of this is going to happen. And that's where we're going to start right now. So the first thing we want to do is to be able to enable our application to download data and ultimately parse this XML that's going to be passed into it. So luckily for us, within IOS, there's actually a class that's gonna help us do just that. So I'm going to create a new instance of this and we're gonna call it a parser. And the type that we're gonna use here, the class that's gonna be available to us is called an NS XNL parser. Now an NS XNL parser is going to allow us to do two things. Download data from the Internet, and then parse it, obviously, if it's XML. So we're going to say that we want a new instance of our parser, and then we're just gonna initialize it to an empty or a default instance of this particular class. So we're gonna modify this a little bit later, but for the time being. We're just kinda getting started. Now in order for us to also get started, we're gonna throw in here a, not really throw away code, but just something that we can use to kind of get started. So we've created a property here that has our parser in it, but we're also ultimately gonna need a URL that's gonna contain. The URL that has a reference to our data that we want to download that we're ultimately going to download. And, we're going to use this as an example here. If you go to the Apple website and you take a look at apple.com/rss, they have a whole slew of RSS feeds. And about halfway down, you have the Apple developer news feed, which is. A pretty good one to keep on top of, especially if you're continuing to learn how to develop applications for the IOS platform. So this is our URL that we need to pay attention to. So developer.apple.com/news/rss/news.rss. So I'm just gonna copy this for now. And we'll head back over to our application. We're going to create a feed URL, which is going to be our property name. And this is going to be a string. And I'm going to leave it as a string for a particular reason here, and you'll see why in just a second. So now, we're going to pop down into our view did load and I'm going to get rid of all these comments here. And if you remember the view did load method here, function is going to be called as soon as the view controllers starts up and presents the view to the user, so as soon as it begins to load that information to show to the user. That's when this particular method fires, and it's typically a good time to start doing some of your wiring up code. So, I'm going to initialize our feed URL and I'm going to set it to the URL that we downloaded, or that we were showing to the developer news feed. So now we have this guy. So what I want to do is I want to then give this to URL the parser to let it know where it needs to go and retrieve the data from. As you can see here, I just kind of gave it an initialization of the, the basic initializer here. The results of that which doesn't really contain any data yet. So I'm going to within my view did load I'm going to create a new instance of my parser and we're gonna set that equal to NSX parser. And if you take a look at the different initializers you have here, you have the empty one, which we used, but then you also have three other ones. You have the contents of URL which allows us to give to it a URL. Then it's gonna go and ultimately download and parse. Then we have some that are gonna allow you to actually parse data that you all ready have, that you've all ready downloaded possible in an NS Data type and then also be able to retrieve something from a stream. So in our case. We're just gonna pass to it a URL, but as you can see here, it wants of type an S URL, but our feed here is a string, so how do we change this string to a URL? Now this may seem very simple, very straightforward, but you're gonna run into a lot of instances of this if you're just new to IOS development, of how do I get from one data type to another? Now luckily. We are working with a string and there's a lot of classes that provide initializers that will allow us to do it basically automatically for us. So, what we're gonna do here is we're gonna create a constant in this case since we know it's not gonna change in this particular example because we're just hard coding this data in here. We're gonna create a constant with the let keyword. And this is going to be initialized whereas going to be specified as a NSURL so that's what we need to pass into our initializer here and we're going to initialize this to a new instance of NSURL, but we're going to use the string initializer, we're gonna pass in our feed URL. So now we have a, an NSURL, and then we should just very simply be able to pass this to our initializer. What you're very quickly gonna see, we have two problems here. Now once again, this is gonna be very common if you're just getting started, so you can click on these and they're gonna give you a little bit. Of help. And if it's this one that has the white circle in it here, it's actually going to present a fix for you that you can take advantage of. So it says here that the value of an optional type, NS URL is not unwrapped, so if you are unfamiliar with optionals, or what they are, then I urge you to go back to my previous course. Gonna take a look at the lesson on optionals and it'll do a better job of explaining it in more detail, but for the time being, just think of an optional as a type that may or may not actually have a value of that type. So this is kind of the version of maybe a nullable type, if you're used to that in other languages. That's presented to you in Swift. So what's happening here, if you take a look at this NSURL initializer. You'll see here that it's actually returning a nullable of NSURL because it doesn't know if you're passing in good data or not and so it may not actually provide enough. Information to create an NSURL. But in our case, we know that it is because we're passing it in here. So we don't have to worry about it too much. So then, the way that we tell swift or anything else that's looking at this, especially this little compiler, the way that we say, yes I know it's an optional, but I want you to take the value in there anyway. Us by using the exclamation point, and so if we add that on there, it's gonna say yep, I just want you to take this and tell it it's gonna be an NSURL and that's the end of that. Same thing with NSXML parcer here. We're gonna do the same thing and that's gonna say, okay parser now definitely contains and NSXML parser. There we go. So now what we wanna be able to do is not only have this parser all set up, we wanna say, hey, go get my data and parse it out for me. Well, the first thing that you're gonna think, all right, well, if I take a look at parser, I'm gonna find that on this particular class, there is a parse function. So this guy will actually go out, download the data, and then begin the parsing process. But unfortunately, that's where all of the pre built stuff for you stops. In order to actually get this to work, you have to provide some implementations to tell the parser what to do with the data when it gets it. So, the way that we do that is in the form of a delegate, so I'm gonna say, parser dot delegate is gonna be equal to self, which means I want the delegate of the NSX of the parser to be this particular view controller. Now, if you're unfamiliar with delegates, then I'm gonna give you the. The very simplified version right now. And delegate is away within the IOS platform to say that, there I know that there is going to be some points during the processing of this particular class. And in this case I'm talking about a parser, where it's going to need me to do some work. Or it needs something to do some work to actually finish. The process of all those downloading and parsing business. And what a delegate is, is it, is a contractual agreement that's going to say, I agree to implement those particular functions or methods or however you want to look at them. To provide that functionality to this particular class. So in order to do that, we need to come up to our class declaration up here, our class feeds table view controller, and we need to add on to the end of this, after UI table view controller, a comma and say that I agree that this class is going to be an NS parser, NSXML parser delegate. Now this little red guys going to go away and now we're going to say okay. I will be your delegate. Whenever you hit one of those points of needing some additional implementation, I am going to provide them to you. Now, we don't really do anything with it yet, but in the next lesson Iim gonna show you how to actually get in there and use. The functions within the NSPART, NSXML parser delegate to actually start to build up this view controller and then ultimately finish this whole parsing process and actually see some data min, manipulation happening.

Back to the top