Objective‑C categories omitted in release builds due to dead‑code stripping
0 reputation · 29 Nov 2021, 13:02 UTC
Goal
Confirm category methods from a static library are retained in the final binary for production deployments.
Context
In debug builds the app loads the category on NSString and the custom method works. After shipping to the App Store, the same code triggers an unrecognized selector exception. The library is linked normally, but the category object file may be dropped by the linker when it is not referenced directly.
Uncertainty
The default linker behavior in release mode performs dead‑code stripping and ignores object files that appear unused, unless the -ObjC or -force_load flags are set. The documentation states that categories are preserved only when the linker is forced to load all Objective‑C symbols.
Questions
- Should the
-ObjCflag be added to every target that imports a static library containing categories, or can we selectively apply it? - What are the trade‑offs of using
-force_loadversus-ObjCfor large libraries? - Is there a build‑time verification step that can flag missing categories before release?