在静态库中链接 Objective-C 类别
我正在为 iOS 应用程序开发一个插件。我将其编译成 .a 文件,然后由主 xcode 项目使用。
到目前为止,我已经在这个库中创建了 UIDevice 类的一个类别。当我使用这个库运行主项目时,它由于无法识别的选择器而崩溃
-[UIDevice platform]:发送到实例
平台的无法识别的选择器是我通过类别添加的功能之一。
所以我认为它根本没有链接这些函数,并将 ac 函数添加到与 UIDevice 类别相同的文件中,然后从我的代码中调用它。
这次主项目运行良好...所以我想也许是我做了其他事情并删除了 C 函数。但你瞧,由于无法识别的选择器,它再次崩溃了。
我的问题: 为什么 xcode 会忽略类别定义,除非我调用在同一文件中声明的函数?
是否有一个 xcode 设置我可以更改以使其包含 UIDevice 类别中的这些方法,无论我是否从该文件调用函数?
干杯
I am developing a plugin for an iOS application. I am compiling it into a .a file which is then used by the main xcode project.
So far I have create a category of the UIDevice class in this library. When I run the main project using this library it crashes due to an unrecognized selector
-[UIDevice platform]: unrecognized selector sent to instance
platform is one of the fuinctions I added via the category.
So I thought it wasn't linking those functions at all and added a c function to the same file as the UIDevice category then called it from my code .
This time the main project ran fine... So I thought maybe it was something else i did and removed the C function. But lo and behold it crashed again due to unrecognized selector..
My questions:
Why does xcode ignore the category definition unless I call a function declared in the same file?
Is there an xcode setting i can change to make it include these methods from the UIDevice category regardless of whether I call a function from that file or not?
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看使用类别构建 Objective-C 静态库:
资料来源:@albertamg(链接静态库中的 Objective-C 类别)
Check out Building Objective-C static libraries with categories:
Source: @albertamg (linking objective-c categories in a static library)
我也遇到过同样的问题。在子项目中定义的类别中定义的方法导致无法识别的选择器异常。 (事实上,这表现为无法在 Interface Builder 中指定 UILabel 子类;XIB 包含 IB 中显示的类(UILabel 或 UIView,取决于我拖到那里的内容),而不是我输入的类,并且看起来像一个奇怪的 XCode bug。)
对我有用的解决方案是使用 -force_load:
在左侧面板中,选择您的主项目(根项目)。在右侧,您将看到
PROJECT
和TARGETS
。选择目标
。转到“Build Settings”(在上方栏中)--“Linking”--“Other linker flags”,假设您的子项目名为 XXXXX,添加
-force_load ${BUILT_PRODUCTS_DIR}/libXXXXX.a
(该项目有两个子项目,“调试”和“发布”,但您单击此复合项目,以便同时影响“调试”和“发布”)。
请注意,-force_load 适用于单个库,您可能需要为每个子项目库指定单独的 -force_load。
I have had the same problem. A method defined in a category defined in a subproject resulted in an unrecognized selector exception. (In fact, this manifested as inability to specify an UILabel subclass in Interface Builder; the XIB contained the class shown in IB (UILabel or UIView, depending on what I dragged there), rather than the class that I have typed in, and that looked as a weird XCode bug.)
The solution that worked for me has been to use -force_load:
In the left-hand panel, select your main project (the root item). On the right, you will see
PROJECT
andTARGETS
. SelectTARGETS
.Go to "Build Settings"(in the upper bar) -- "Linking" -- "Other linker flags" and, assuming your subproject is called XXXXX, add
-force_load ${BUILT_PRODUCTS_DIR}/libXXXXX.a
there (the item has two subitems, Debug and Release, but you click on this compound item so that if affects both Debug and Release).
Note that -force_load works for a single library, and you may need to specify a separate -force_load for each subproject library.
我遇到了这个问题,花了近1个小时才解决。感谢上帝!已经完成了。定义的方法应该是静态类型!
I had this issue and spent near 1 hour to resolve it. Thanks god! it's been done. Methods which are defined should be static type!