David
David

Reputation: 2810

Objective-C categories with the same name

I have a set of data classes that I'd like to extend with my own methods by creating categories unique to each class.

When declaring a category, does each category need to have a unique name, or can I reused the same name.

Example. Say I have a List class and ListItem class and I want categories on both. What I'm doing now is declaring as follows:

In List+Additions.h

@interface List (Additions) ...

In ListItem+Addtions.h

@interface ListItem (Additions) ...

Is that ok? I can't tell if its consistently working. What is considered the category name? Is it "Additions", or the combination, "ListItem+Additions".

Upvotes: 2

Views: 333

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90571

The category names are List (Additions) and ListItem (Additions). So, the two categories are separate and independent.

Something like "ListItem+Additions" is a common convention for the file names which define the categories, but neither the compiler nor the runtime pay any attention to that.

Upvotes: 2

Sailesh
Sailesh

Reputation: 26207

Is that ok? I can't tell if its consistently working. What is considered the category name? Is it "Additions", or the combination, "ListItem+Additions".

Yes, it is ok.

Both the categories have same name: Additions, but one is a List's category, and other is a ListItem's category. So there is no clashing of names. This is true even if you would have put both the categories declarations in same file.

Upvotes: 2

Related Questions