Understanding Category in Objective-C





What is Category in Objective-C?
  • Objective-C allows you to add your own methods to existing classes through categories and class extensions.
  • A category can be declared for any class, even if you don’t have the original implementation source code (such as for standard Cocoa or Cocoa Touch classes).
  • Objective-C Categories provide the ability to add functionality to an object without subclassing or changing the actual object. 
  •  A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.
  • Subclassing is one way to add functionality to an object, but avoiding unnecessary subclassing by using a category will help reduce the amount of code and keep your projects more organized. 

When to USE Category in Objective-C ?

There are several uses of a category, each of which add methods to a class (note: methods only, not iVars)

Extending an existing Cocoa class

 This lets you add your own methods to an existing class. For example, if you want to extend NSString to apply special capitalization, you could create a new class called, say NSString+Capitals. in the NSString+Capitals.h you would have:

 @interface NSString (Capitals)

 -(NSString *)alternateCaps:(NSString *)aString;

 @end

 and in NSString+Capitals.m you would implement the method

 @implementation NSString (Capitals)
 -(NSString *)alternateCaps:(NSString *)aString
 {
 // Implementation
 }

Private methods on a class

 This is the same as above, except that the extra methods are declared and defined in the implementation file (.m) Usually a way of having private methods - because they are not in the .h file (which is the one #imported by other classes) they are simply not visible. In this case, the implementation of the methods are done in their own implementation block. e.g

 // someClass.m
 @interface someClass (extension)
 -(void)extend;
 @end

 @implementation someClass
 // all the methods declared in the .h file and any superclass
 // overrides in this block
 @end

 @implementation someClass (extension)
 -(void)extend {
 // implement private method here;
 }

Class Extension (New for 10.5 Leopard)

 A simpler way of having private methods. In this special case, the category name is empty and the private methods are implemented in the same block as all the other class methods.

 // someClass.m
 @interface someClass ()
 -(void)extend;
 @end

 @implementation someClass
 // all the methods declared in the .h file and any superclass
 // overrides in this block
 // Implement private methods in this block as well.
 -(void)extend {
 // implement private method here;
 }

 @end


 When NOT to USE Category in Objective-C?

Although the Objective-C language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from doing so. A category is not a substitute for a subclass. There are several significant shortcomings to using a category to override methods:

  • When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that exists in the category's class, there is no way to invoke the original implementation. 
  • A category cannot reliably override methods declared in another category of the same class.
  • This issue is of particular significance because many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.
  • The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances of NSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes.

SEE ALSO

No comments:

Post a Comment