5_Objective-C Categories

If you need to add a method to an existing class, perhaps, to add functionality to make it easier to do something in your own application, the easiest way is to use a category.

The syntax to declare a category uses the @interface keyword, just like a standard Objective-C class description, but does not indicate any inheritance from a subclass. Instead, it specifies the name of the category in parentheses, like this:

Syntax

@interface ClassName (CategoryName)

@end

Characteristics of category

  • A category can be declared for any class, even if you don’t have the original implementation source code.
  • Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class.
  • At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.

Example of category

In our example, since we just have a single class, we have not included any header files, in such a case we should include the header files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: