Gokul
Gokul

Reputation: 1226

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?

Upvotes: 5

Views: 347

Answers (2)

Kreiri
Kreiri

Reputation: 7850

Whichever implementation of method with same name "wins" doesn't have anything to do with category names. The "winning" one will be the one loaded last, and whichever one will that be isn't known until runtime, because load order is non-deterministic.

Upvotes: 0

Droppy
Droppy

Reputation: 9721

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.

Upvotes: 4

Related Questions