Septiadi Agus
Septiadi Agus

Reputation: 1775

What happen to categories on the same name?

Say A is subclass of B

Say A has a category doThis.

B also has a category doThis.

Say I did

B* b = [[B alloc]init];
[b doThis];

Is there a guarantee that doThis declared in B+doThis.h will be the one called instead of A+doThis?

Sample:

in NSManagedObject+somecategories.m

+(void)vLoadBookmark
{
   //Just empty
}

Latter

in BusinessObject+somecategories.m where BusinessObject is a subclass of NSManagedObject

+(void)vLoadBookmark
{
   //Do something

}

If one day I called [someBusinessObject vLoadBookmark] will //Do something be reached?

Upvotes: 1

Views: 84

Answers (1)

Jano
Jano

Reputation: 63667

If a category overrides a method defined in another category, it is undefined what implementation will prevail.

From Apple's Programming with Objective-C > Avoid Category Method Name Clashes:

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.

Upvotes: 5

Related Questions