Q: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?
A: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?
The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of Objective-C. Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.
To resolve this issue, the target linking against the static library must pass the -ObjCoption to the linker. This flag causes the linker to load every object file in the library
that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that
contain categories on existing classes. Follow these steps to pass -ObjC to the linker:
-
In Xcode, double-click the target's name under "Targets" in the Project window.
-
Choose the Build pane from the ensuing Info window.
-
Scroll down to the
Other Linker Flagsbuild setting under the Linking collection and set its value to-ObjC.
Important: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from
loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.
-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is
available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file
in that archive will be loaded.
本文详细解释了当链接包含Objective-C分类的静态库时出现'选择器未识别'异常的原因,并提供了通过在目标链接中传递-ObjC选项来解决此问题的方法。
1万+

被折叠的 条评论
为什么被折叠?



