1

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?

Septiadi Agus
  • 1,775
  • 3
  • 17
  • 26
  • B has method doThis therefore doThis will be called. – stosha Jun 19 '13 at 02:01
  • No. doThis is also a category of B – Septiadi Agus Jun 19 '13 at 02:14
  • 1
    I think you have your subclass vs superclass terminology confused. If `A` is a subclass of `B` and you instantiate `foo = [[B alloc] init]` it is absolutely impossible* for `[foo anything]` to call `A`'s implementation, since it's not a superclass. (*: barring some really, really inadvisable introspection) – Kevin Jun 19 '13 at 02:16
  • Give us a very short, minimal implementation of what you intend, so it's clear. – Kevin Jun 19 '13 at 02:18

1 Answers1

5

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.

Jano
  • 62,815
  • 21
  • 164
  • 192