Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of Category name in iOS

Can anyone please tell me the significance of category name when we create a category?

I know that the compiler uses this to identify and match the implementations with interfaces. Is there any other use for it?

What if we create 2 categories with different names implementing same method in 2 different ways. Example:

@interface NSString(Good)
- (BOOL)isGood;
@end
@implementation NSString(Good)
- (BOOL)isGood
{
return TRUE;
}
@end


@interface NSString(Bad)
- (BOOL)isGood;
@end
@implementation NSString(Bad)
- (BOOL)isGood
{
return FALSE;
}
@end

And now in the program I create a string

NSString *goodString = @"GOOD";

I got the output of [goodString isGood] as false.

I want to know why and how the name of category is involved in this?

like image 784
Gokul Avatar asked Sep 29 '22 03:09

Gokul


1 Answers

With respect to the category names, according to this article the only restriction is that they don't conflict with other category names within the same class.

With respect to the categories using the same method names, according to the Apple Docs, it's undefined which method will be called at runtime.

like image 197
Droppy Avatar answered Oct 21 '22 14:10

Droppy