1

I have added some methods to existing class in engine by category. This category is declared and implemented in my separate files. Then I include these files (but all engine files stay not modified, so only original declarations are included in engine). Engine is built into static lib and linked with my app. When I call a method of my category the app crashes with error "unrecognized selector sent to instance...". But if I declare category in file with original engine class all works.

Why selector of category is not recognized if it's declared and implemented in separate files? Does order of including files matter?

brigadir
  • 6,874
  • 6
  • 46
  • 81
  • How have you declared the category? Could you add some code so we can see that you have properly declared it? – Abizern May 17 '11 at 11:19

2 Answers2

3

This is a linker bug where category methods declared in their own compilation unit are not correctly linked into an app. See the technical note from apple here:

Building Objective-C static libraries with categories

You must either specify the linker flag -all_load in your application, or a 'hacky' technique would be to define a macro which will define a dummy class and implementation, and call that macro in each category implementation:

#define FIX_CATEGORY_LINKER_BUG(name) \
    @interface FIX_CATEGORY_LINKER_BUG_##name @end \
    @implementation FIX_CATEGORY_LINKER_BUG_##name @end

And use it as follows above your category implementation:

FIX_CATEGORY_LINKER_BUG(NSStringMyAdditions)

@implementation NSString (MyAdditions)
// ...
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
0

You need to set some flags for the linker... please refer to What does the -all_load linker flag do? for details.

Community
  • 1
  • 1
Eiko
  • 25,601
  • 15
  • 56
  • 71