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
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."
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With