7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Creating Models

Now that you understand the basics in setting up your project and getting the UI flow created, we need to start dealing with data. Since iOS is inherently based on the MVC (Model-View-Controller) pattern, we now need to focus a little on the M and create some Models.

3.1 Creating Models

So far to this point in the course we've spent a lot of time going through and creating the views and the controllers of our application. And if that seems somewhat familiar to you or if I start talking about views and controllers, then you might start to think about, if you've ever heard of the MVC pattern, or Model-View-Controller pattern. And that's really what almost everything within the iOS platform, when you're creating these applications, is built using. We've already got the second two-thirds of that pattern starting to kind of take shape, in the form of our views, which you can think of as being the table view here, these prototype cells on these first two view controllers and then the web view here on the last. So those are the views that you're seeing there. The controllers are ultimately what these views are sitting in if you are thinking of it from that perspective, from a storyboard perspective on this layout here. And we created those controllers or created the subclasses of those controllers for the feeds table, the articles table and then ultimately the web view controllers. So those are gonna be our controllers and that's going to be the binding between the view what's actually presented to the user, and the first third of that acronym, the models. Now that's what we haven't really started to do yet and really to this point, you could have picked either language, Objective-C or Swift, to do this project. And to this point, it really wouldn't have changed very much other than the code would have looked a little bit different in these view controllers that we've created. But other than that, it really would've been very similar, so now is where we're really gonna start to really lean towards Swift as our language of choice. So, we need to start creating these models to contain the data that we ultimately want the controller to send to the view, to present to the user. And that's what we're gonna do in this lesson. So anywhere within our SwiftReader folder, we can right-click and select New File, so from within here, I'm gonna select Cocoa Touch Class, just like we've done before. Make sure you're under the iOS family and source. Selecting Cocoa Touch Class. We'll select Next. And in this case our class name is going to be one of two things to start. So, within the domain of this project, of this application, we really have two different things we're working with. We have the concept of a news feed. Now a news feed is the aggregation of several different articles out on a site somewhere typically in XML format since we're kind of dealing only with say, RSS or maybe Atom feeds in this particular course. So we have the concept of a feed, which is going to contain an aggregate of all of these different articles, or these links to all of these different articles, and possibly descriptions and, and things of that nature. But we'll take a look at an example of that shortly. So we're going to need a class that's going to represent that feed, that parent level object, as well as each one of those articles that are going to be a child of that feed. So we're gonna start to create these classes like so. So, typically, what I'm creating these types of classes that are, what I would consider models, you can go one of two routes. You could create a folder in here and call it models or do however you'd like. I'm going to, for simplicity's sake in this case, I am going to call each one of these classes whatever name I choose. In this case, it's gonna be Feed, but I'm also going to append the word Model onto the end of it, and you're gonna see why later on, when we start working with core data a little bit, why I'm, so, being very explicit. And this is a model. This is data that is ultimately going to be presented to the user somehow so, so it's very easy for us to realize that this is going to be the model portion of the Model-View-Controller pattern. Now make sure that the subclass is of NSObject, so if it's not that right now, make sure you switch it over to that. And then we're gonna make sure that our language is Swift. So we'll select Next, and now you get to pick where we want this to be. I'm gonna leave it in the SwiftReader folder and select Create. And there you have it. We now have a very simple, empty class called FeedModel, whose base class is NSObject. And we're gonna start to fill it in with some things. Now, if we were to take a look and I'm gonna show you an example of a, of a news feed and what this is going to look like. So at this point, if you were to open up your favorite web browser and head over to tutsplus.com/blog, you're gonna see a lot of nice blog entries from the people at Envato and everybody that contributes to Tuts+. You'll see some very nice content here. But if you take a look over to the top right, you're gonna see this little icon here that says Feed. So if I were to click on this, you're gonna see a whole mess of XML. Now this is basically a feed that has a lot of very common elements and attributes that are kinda defined in different places depending on what type of feed it is. In this case, as you see, it ends here in atom so this is a feed that's gonna kinda follow that paradigm. But there's also other different times, there's OPML, there's RSS, there's a lot of different kinds but ultimately they all kinda follow very similar structures, just different naming conventions. So what we're going to do is we're going to create a class that's gonna somewhat model this basic concept that will ultimately allow us to parse out and present some very basic feed data to the user. Now the type of feed parser that we're gonna create is not going to be 100% compatible with all different feeds that you're gonna find out on the Internet. But, it's going to get you pretty far, I think. So what we're going to do is we're gonna create these feed model. We're gonna give it a couple of properties. First of all, most of these feeds or these different types of feeds are gonna have a title. So we're gonna create a property called title, and we're going to make sure that this is going to be a string, and we're just going to initialize it to a new string. So that's the basic concept here, what we wanna do. We wanna make sure this feed model has a title. We also want to make sure that this feed model has a URL, because ultimately we need to know where this feed lives. So if you were to go back to that website, if you're to just take a look, we have tutsplus.com/blog.atom. So that would be the URL that we're talking about, if we were to parse this particular feed. So the URL is once again also gonna, we're gonna keep it rather simple. We could make it something else, but we're gonna leave it as String just to keep it relatively straightforward. Now the last piece that we're gonna be concerned about at least for this application, is gonna be the actual articles. Now the articles of themselves are gonna have different properties and, and things about them that we're gonna want to save in a certain way. Also in some different properties, so we're gonna actually create a new model to take care of that. So once again we're gonna come in here, we're gonna right-click and we're gonna select the New File > Cocoa Touch Class, select Next and this is gonna be our article model. Once again it's gonna be a subclass of NSObject, it's gonna be in the Swift language. We'll select Next. We wanna make sure that it's in our SwiftReader folder, we'll select Create, and there you have it. We now have our ArticleModel just like we had our FeedModel. So once again there's a few different aspects that you're going to find that most articles, regardless of the feed type, have in common. And that's also going to be a title to start. So we're gonna create a title property. Also going to be a string and we're going to initialize it. That's going to be a String and we're also going to initialize that to an empty string. Then we're also going to create a URL or a link in this case we could call it. And this is also going to be a String because ultimately what you're going to get within these RSS feeds when it comes to the articles is, is that gonna give you all of the article text. It's just going to give you a link out to another site somewhere where you can actually read the actual article itself. And then typically what you're also going to see is some sort of publish date, or publication date. We're gonna call that pubDate. Now this one we're actually also going to store as a String because we're, we'll play around with the formatting of that a little bit later so you can see how to format dates when it comes to Swift and iOS development. So now we have this basic ArticleModel here. We have our FeedModel here. Now we wanna add into our FeedModel, the concept of this article that we created. Now, a feed can have multiple articles, so we're gonna do that by creating a list, or an array of articles. So, we're gonna call this property articles. And we're going to initialize it with the square brackets and we're going to call this of type ArticleModel and then we're going to initialize this just like we've done before as an empty array of article models. So there you have it. Now we've created two model classes, our FeedModel and our ArticleModel. And this is ultimately what's going to contain the data that we're going to have passed to our view or rendered via our view by our view controller to actually show some data to the end user. So now, over the next couple lessons, we're actually going to start to work on parsing data, actually going out to the Internet, downloading some data. Creating these models and then passing that to the view, so we can actually start to see some things show up on the screen. So we're starting to get to the point where we're gonna start reaping the benefit of our work, so definitely hang in there. We're gonna start going fast and furious into presenting data to the user in the next couple of lessons.

Back to the top