Reputation: 453
When I'm learning categories. It says the new method is added to all the instances of the class. Even class method does the same right? We have class method that is available to all the instances. So when to use category and when to use class method?
Upvotes: 0
Views: 802
Reputation: 269
ok, so lets look at an example. Lets say you use NSArray as your primary collection object for your data. So this means that most of the time, your data is being passed around in an NSArray.
NSArray *myArray = @[@"obj1",@"obj2",@"obj3"];
At the same time, lets say you perform certain operations on your data set all over in your application. For this example lets say you want to randomize all of your data in your array. How could you achieve this?
Option 1 (Custom Subclass - Instance Methods): Write a class that acts as a "Helper" that implements common operations on your data set using INSTANCE METHODS.
Ex) MyDataHelper Class
@interface MyDataHelperClass : NSObject
{
-initWithData(NSArray*)array;
-(NSArray*)randomizeData
-(NSArray*)sortData
...
}
While there is nothing deathly wrong with something like this, it does seem like extra work that you have to go through that will get copied all over the place. You need to init your helper class, and then call the instance methods to manipulate your data.
Ex)
// Init your helper
MyDataHelper *helper = [[MyDataHelper alloc]] initWithData:myArray];
// Call your helper methods
NSArray *randomArray = [helper sortData]
Option 2 (Custom Subclass - Class Methods): Write a class that acts as a "Helper" that implements common operations on your data set using CLASS METHODS.
@interface MyDataHelperClass : NSObject
{
+(NSArray*)randomizeData:(NSArray*)data
+(NSArray*)sortData:(NSArray*)data
...
}
Class methods are methods that can be called from a class without requiring an instantiation of the class. Most of the time they are used to provide convenience for operations that you may call often etc...
Ex)
NSArray *randomArray = [MyDataHelper randomizeData:myArray];
Notice how the usage is much more simplified, but is there a better way than this?
Option 3: (Categories - Instance + Class Methods) Use a category to add Instance Methods or Category Methods to an existing class.
A category is like a way to add class and instance methods to any existing class. When you do this, if you import your category, you will be able to call your added methods off of the extended class.
In this example, our data set is an NSArray. If we add a category called DataHelper+NSArray, we can add our helper methods directly on the NSArray object.
Ex)
@interface NSArray (DataHelper)
- (NSArray)sortData;
- (NSArray)randomizeData;
@end
@implementation NSArray (DataHelper)
- (NSArray)sortData
{
...// Calling self, is the NSArray instance (myArray)
}
- (NSArray)randomizeData
{
...// calling self, is the NSArray instance (myArray)
}
@end
Notice how the usage makes much more sense now.
Ex)
NSArray *sortedData = [myArray sortData];
Upvotes: 2
Reputation: 52538
Categories and class methods are totally different things. The one sentence that you read, you misinterpreted completely. Class method are not added to any instance of a class. Instance methods are added to instance of a class, class methods are added to the class itself.
A category adds further instance methods that are not available within the class definition itself. What's nice in Objective-C is that you can add a category with additional instance methods even if the class itself isn't available to you.
Upvotes: -1