How to Create Class Categories in Objective C
Note: Video is Best Viewed in Full Screen Mode
As I began refining some sample code for making Universal Apps for the iPhone and iPad, I sought a way to make my code as concise as possible. I needed to determine if the app was running on an iPad or iPhone/iPod Touch and use it to my advantage. This basic information is contained in the UIDevice class.
did not do precisely what I needed to do. I needed something specialized. I wanted to add new functionality to the UIDevice class as if it had been there all along.
Looking for Solutions
A few months back I ran into a code sample that adds functionality to the NSDate class called NSDate+Helper. This class, created by a programmer named Billy Gray was my first exposure to a wonderful feature built into Objective C called Class Categories. When I saw how one could do this, it set the wheels turning in my mind at all the possibilities!
In this tutorial I am going to show you how to use class categories, and for our example we are going to add functionality to the UIDevice class for use in ultimately creating a sample Universal App.
Sample Code Info:
Getting Sample Project Source Code & Running it in the Simulator
It would be handy for you to download a copy of our Universal App project to look over and try out. In order to really use it you will need to have an Intel-based Mac computer, and XCode 3.2 or better installed on it. Once you have done this, you will be able to run it in the iPhone Simulator.
Make sure when running on the simulator that you set the proper build version before doing a build:
Simulating the app running on an iPhone:
Simulating the app running on an iPad:
Running the Sample Project on an Actual Device
If you want to run the project on an actual device rather than just the simulator, you will need to be a paying member of the Apple Developer Network ($99 a year at the time of this post). And you will need to have gone through the hoops of setting up your certificates, app record, and provisioning. … We are not covering any of this getting on the device stuff in this tutorial.
At least you can run the project in the iPhone Simulator for free!
What is a Class Category?
- You use class categories to add new functionality to an existing class. This could be one of your own classes, or it could be a class in Apple or someone else’s API.
- You do not have to sub-class the class you are adding to.
- You can add the new functionality in such a way (inside your project) that it seems that the functionality was there all along!
Step 1: Come Up With A Category Name
When creating a class category you are adding some sort of new functionality to an existing class (without touching any of the code actually in the existing class)! If you were to describe in a general way what that new functionality was, how would you phrase it? What “category” would it fall under? This is something you make up. I make up some sort of camel-cased phrase for my category name. In the case of my new functionality to add to UIDevice, I thought that MyDeviceType captured it well. Perhaps if I had thought about it longer I could have made up something better! But that is what I came up with so that is what I used!
Step 2: Create a Group in Our Project to Place our Class Category in (optional)
The first thing you may want to do is create a new group (folder in your project) to place it in. This is optional, but it can make finding it easier as well as providing some form of internal documentation.
So, for the sake of argument, let’s say that this is what you want to do. Let’s go through the steps needed to do this:
– First of all click on your project file item (in examples shown in the screen shots, this is “classcat”).
– Then control-click on it again to bring up a pop-up menu.
– Click the Add option at the top of the menu…
– Then click the New Group option (see screen shot below)
- Add new functionality to existing classes (especially API type stuff) like we did in this tutorial.
- Break a large class up into separate files grouping various blocks by function “category.”
- Can you think of anything else? 🙂
Hope you find this tutorial useful. Any questions? Ask away. If you could rate my Youtube video that would be great too!
In the next tutorial we will be talking about the structure of a Universal App.
Originally published on iPhone Obsessed blog