Categories in Objective-C

Mark Wahlbeck
A free video tutorial from Mark Wahlbeck
CEO Devslopes | iOS | Android | Freelancing
4.3 instructor rating • 15 courses • 293,261 students

Lecture description

Learn how to extend functionality of classes with Objective-C categories - similar to Swift extensions for iOS apps

Learn more from the full course

Objective-C Crash Course for Swift Developers

Everything you need to know about modern Objective-C to use it professionally

08:19:43 of on-demand video • Updated February 2016

  • Write code in Objective-C
  • Convert Swift iOS Apps into Objective-C
  • Convert Objective-C iOS Apps into Swift
  • Build Simple Node & Express Servers and APIs
  • Communicate between iOS apps and Servers
English [Auto] Here we go again. It's going to be so much fun writing some code. Ex-coach open it up. Now by the way if you didn't know this is Mark Preston of soap's dot com you like yeah I heard it the last thousand times. Well now you know so here to talk today about categories object to see category's K.. We're going to create it basically a category is just an extension of a class. OK. I actually use these quite a bit on my professional projects. For instance one category I use all the time is an extension to N.S. date. I'm not happy with all the data. Well they have data formatting tools but it doesn't always give me or I need because I like to use one type of format usually it's ISO date format I use and on the back in the front end and so I want to do all these tools that you know the month the years and it just formats that free up exactly as you need. And as you start working with it you're can understand how it's a pain working with dates. So I created this category. Basically it's an extension in his day and all I have to do is include it my project and it adds extra features to the state class so I can use it right on my and my project. So let's go ahead and clear out that new file. It's really easy. So first we're in a credit class let's call this let's call this Honda because it's going to be a Honda. All right. And also this Honda has some properties on its strong and as string. This one is probably the model right at property atomic. Strong will say any number call this Miles. OK. Probably has a function too or multiple functions right. So void. Maybe we want to increase the miles so increase Miles you know to odometer this plane around to Odon it or another one that's probably like Dr. Wright has We've got some some generic function here and some properties Let's go ahead implement these functions like so personally blank for now. OK. So the idea is you have a class right and it has core functionality. OK here's here's. Here's our core functionality for a class. Can you get a nice hand there right off the lot. It's pretty brand new car. You know that you bet. This is a Honda. That's a good relie old reliable car. But then all of a sudden you know sloppy joe gangster guy with his hat to the side you know in his baggy pants skinny white guy right. He's like you know what I want to souped up my Honda. You know so he's like yeah let's create a category for that. And so he's like all right. So let's go ahead and create a category. And I believe we select Objective C file here and you click next and instead of empty file you're going to pick a category by the way. Extension is very similar to category. We're not going to cover it but basically an extension is just like a category an extension to a class except you can only use on your own classes whereas a category you can use on other people's compiled source code to extend the functionality and features. OK. So this category is going to be called souped up. If that's how you spell souped up pay and file type category grade class we want it to be of class Honda Okay category of Honda. So you know you're never that old school offspring songs like pretty fly for a white guy like whenever I see those guys in those harness that's what I think about like you know any white guys who like think they're cool with those souped up Hondas. Anyway so cool sloppy joe gangster that's his gangster name by the way. He's I want a super. My Honda. So what does he do. He does some really stupid stuff. OK. This is what he does. OK. Ad ugly spoiler he adds a function here called an ugly spoiler. OK then he has something else in his category. He can do what he wants. OK add up the spoiler. He also wants to add gold plastic spinner rims. OK. That's cool. OK. And so yeah. And then for some reason. OK. He wants to scrape off half the paint right. Why is it with friggin Hondas and these souped up Hondas like half the car all the paint scraped off like you're showing that you're doing something. Oh. And lastly there is another function we want to add. You know called Ad horrible sounding muffler cap. You know what it is. So this category is adding features to the Honda car. OK. And this could have been a string of course as well too that we're adding that we're adding a category two. And of course you can add properties as well too. So you know we could add a property atomic. And this one is going to be strong and let's make this in this number and it's going to have a wow factor right. OK. So this property also has a wow factor and it's probably an animal like negative one or something. But anyway my point is Properties and functions are methods you can extend to a category. So you know slobby Joe gangster's doing some really cool stuff here. And let's go and implement these functions now. Come here in the implementation file boem boem OK. All right. You know and of course these would do things right. Do stuff. I don't need to write it out right. These are functions that would do stuff. OK. So my point is what is it yelling at me for all we need to since we're using the category we need to use set well factor to be defined. So you just do that here. Boy set wow factor. OK. Now and then we need to OK. So yes self-taught well factored Well factor we got is the dynamic keyword for that property. If you wanted to if you wanted to have if you wanted to have a property in your category most of the time is not common to have properties in categories. It's more so extended functionality. OK but anyway. So this is cool right. So we've got a Honda and we've got the Honda souped up and what we can do is here in our view control and now we can import. Well first off let's import the Honda let's just see how that works with him. So we've got a regular old Honda was a great one. OK. So we're in say Honda Honda equals Honda alloca and initialized right. So there's our Honda. And you know it has its default functionality such as increased miles drive. And then we've got these ones over here such as an ugly spoiler. Let's see if we can add an ugly spoiler. Can I do that. So if I say Honda ad ugly you can't. All we have is access to the core features and functionality. So now slobby do gangster. You know he imports his category Honda plus souped up age. OK. Now let's see what kind of damage you can do. Honda at ugly spoilers so now you can see if you're adding functionality to a class that never existed before. OK. And you can of course do this in a string like if you wanted to have a bunch of formatting of characters or things where you wanted to create something that truncates out decimal periods and converts strings and all kinds of things you can do that industry category you make categories for all the different objective c classes for all your own classes. Really cool stuff. And so all you can do damage you're like Mr sloppy joe gangster did bye you know and by adding you know gold plastic man so he can do all kinds of damage now and destroy your perfectly made class. So enough of that. That's category's very similar with extensions except extensions can only be done on your own code. You actually have the full source code and the questions. All right. That's it. Mark Price at Dove slopes duck. See you later. And remember to get your free life help. Go to desktop's dot com and click on the chat room with thousands of students and they're helping each other out. Also click on the forums. This is where you can search for answers to your code problems. You can also post your own issues so head on over now.