Refine Objective-C categories in a separate file

I added some methods to an existing class in a category by category. This category is declared and implemented in my individual files. Then I include these files (but all the engine files remain unchanged, so only the original ads are included in the engine). The engine is built into the static library and linked to my application. When I call a method of my category, the application crashes with the error "unrecognized selector sent to instance ...". But if I declare a category in a file with the source engine class, everything works.

Why is the category selector not recognized if it is declared and implemented in separate files? Does file inclusion order support?

+1
source share
2 answers

This is a linker error in which category methods declared in their own compilation unit are not properly bound to the application. See Apple Technical Note:

Creating Objective-C Static Libraries with Categories

You must either specify the -all_load linker -all_load in your application, or the hack method would have to define a macro that defines a dummy class and implementation, and call this 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 to implement your category:

 FIX_CATEGORY_LINKER_BUG(NSStringMyAdditions) @implementation NSString (MyAdditions) // ... 
+3
source

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

0
source

All Articles