Making Cocoa Touch Your Own

As you saw in Chapter 2, categories in Objective-C are very powerful mechanisms for extending existing classes (Categorically Speaking). Flashlight Pro has three categories. Two are used to create and set colors with a brightness value, and a third is used to conceal a view onscreen.

UIColor+Brightness

The first category you're going to look at is UIColor+Brightness. This category takes some code you found on the Internet, wraps it in Objective-C, and integrates it with the existing UIColor class.

Naming conventions

The first thing you notice about the category is the name of the source file. Since these categories tend to be shared among projects, a naming convention is important to avoid conflicts. You'll find that your own categories get a lot of reuse, and you can also find a lot of great open source ones.

In the convention used here, the names are derived from the class being extended and the name of the category. For this category, the @interface is defined as:

@interface UIColor (Brightness)

That makes the name UIColor+Brightness. If the category name had been "(Chockolicious)," the files would be named UIColor+Chockolicious instead.

Note

Some developers use a dash as a separator instead of a plus sign. The separator isn't really important; it's the unique combination of the original class and the category that matters.

Wrapping it up

If you look at the @implementation, you see a lot of C code that converts between the RGB and HSV color spaces. (The source ...

Get iPhone App Development: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.