gpt4 book ai didi

ios - 在静态库中链接 Objective-C 类别

转载 作者:IT王子 更新时间:2023-10-29 07:41:59 24 4
gpt4 key购买 nike

我正在为 iOS 应用程序开发插件。我正在将它编译成一个 .a 文件,然后由主 xcode 项目使用。

到目前为止,我已经在这个库中创建了一个 UIDevice 类的类别。当我使用这个库运行主项目时,它由于无法识别的选择器而崩溃

-[UIDevice platform]: 无法识别的选择器发送到实例

平台是我通过类别添加的功能之一。

所以我认为它根本没有链接这些函数,而是将一个 c 函数添加到与 UIDevice 类别相同的文件中,然后从我的代码中调用它。

这次主要项目运行良好...所以我想也许是我做了其他事情并删除了 C 函数。但是你瞧,由于无法识别的选择器,它再次崩溃了..

我的问题:为什么 xcode 会忽略类别定义,除非我调用在同一文件中声明的函数?

是否可以更改 xcode 设置以使其包含 UIDevice 类别中的这些方法,而不管我是否从该文件调用函数?

干杯

最佳答案

查看 Building Objective-C static libraries with categories :

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 -ObjC option 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.


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.

来源:@albertamg ( linking objective-c categories in a static library )

关于ios - 在静态库中链接 Objective-C 类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6820778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com