iosobjective-cobjective-c-category

Objective-C defining the same category in multiple modules


I have 2 categories defined in 2 modules:

Module 1: 

Array2D+RawString.m: 

@implementation Array2D(RawString) 

+ (Array2D *)arrayWithRawString {
  // some implementation specific to module 1
}

@end

Module 2: 

Array2D+RawString.m: 

@implementation Array2D(RawString) 

+ (Array2D *)arrayWithRawString {
  // some implementation specific to module 2
}

@end


Both categories are visible only within its own module. What I expect is that, the code in module 1 will the file in module 1, and vise versa.

However, what I have notice is that the module 2's implementation is actually overridden by module 1. Why is it so? How do I deal with this problem?


Solution

  • That's so because that's the way the language defines it. If you define a category with the same name in two places, the compiler/linker will randomly pick one of them.

    You can have categories with different names. Just don't call them both RawString.