Objective-c – Difference between subclass and category?

objective c

Possible Duplicate:
Difference between inheritance and Categories in Objective-c
What’s the difference and use of categories and inheritance?

thanks for your reply,for example we have nsstring class if we want to add methods to that class there is no need to create category for that,just we can subclass it but why we are using categories?Please help on this

Best Answer

Category adds some extra functionality to specific class (for example NSString). You don't need to declare the Object with that specific class name. You only import that category and all the Object implicitly become instance of the category, all the implementation is now available to them.

Where when subclassing, (sometimes you intently need to override the existing behavior/methods or you can add extra functionality too.) you explicitly declare that Object with the type like

MyCustomString *string;

and then all the methods become visible.