Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is “category” in Objective-C?

Tags:

Possible Duplicate:
How does a category work in Objective-C?

I am just getting started with Objective-C and was wondering when and where am i supposed to use category in objective C. is it a class or a protocol? An explanatory example would be of great help. Thanks

like image 585
wOlVeRiNe Avatar asked Sep 14 '11 09:09

wOlVeRiNe


People also ask

How do you declare a category in Objective-C?

Let's create a category that add functionality to UIFont class. Open your XCode project, click on File -> New -> File and choose Objective-C file , click Next enter your category name say "CustomFont" choose file type as Category and Class as UIFont then Click "Next" followed by "Create."

What is category in iOS?

You use categories to define additional methods of an existing class—even one whose source code is unavailable to you—without subclassing. You typically use a category to add methods to an existing class, such as one defined in the Cocoa frameworks.

What is the difference between category and extension in Objective-C?

Use category when you need to want to add any methods to a class whose source code is not available to you OR you want to break down a class into several files depending on its functionality. Use class extensions when you want to add some required methods to your class outside the main interface file.

What is category in Swift?

In Objective C they were called categories, but in Swift they are called extensions. The purpose of both of them are to give additional functionality to existing classes without having to create subclasses.


2 Answers

A category allows you to add methods to an existing class—even to one for which you do not have the source.

Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing

Check the apple doc for the Category in Objective-C

like image 60
Jhaliya - Praveen Sharma Avatar answered Oct 03 '22 23:10

Jhaliya - Praveen Sharma


There is a section in the Objective-C 2.0 programming Language document about Categories and Extensions.

As you are just getting started it's a good doc to have on hand to look things up.

There is also a section on Categories in the Cocoa Core Competencies document, which is easier to get started with, and also has a lot of useful information for the beginner.

like image 45
Abizern Avatar answered Oct 04 '22 00:10

Abizern