- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
你能指导我如何正确地将静态库链接到 iPhone 项目。我使用添加到应用程序项目中的静态库项目作为直接依赖项(目标 -> 常规 -> 直接依赖项)并且一切正常,但类别。静态库中定义的类别在应用程序中不起作用。
所以我的问题是如何将具有某些类别的静态库添加到其他项目中?
一般来说,在其他项目的应用项目代码中使用的最佳做法是什么?
最佳答案
解决方案:从 Xcode 4.2 开始,您只需转到链接库的应用程序(而不是库本身),然后单击项目导航器中的项目,然后单击应用程序的目标,然后build设置,然后搜索“Other Linker Flags”,单击 + 按钮,并添加“-ObjC”。不再需要“-all_load”和“-force_load”。
详情:我在各种论坛、博客和苹果文档上找到了一些答案。现在我试着对我的搜索和实验做一个简短的总结。
问题是由(来自苹果技术问答 QA1490 https://developer.apple.com/library/content/qa/qa1490/_index.html 的引文)引起的:
Objective-C does not define linkersymbols for each function (or method,in Objective-C) - instead, linkersymbols are only generated for eachclass. If you extend a pre-existingclass with categories, the linker doesnot know to associate the object codeof the core class implementation andthe category implementation. Thisprevents objects created in theresulting application from respondingto a selector that is defined in thecategory.
以及他们的解决方案:
To resolve this issue, the staticlibrary should pass the -ObjC optionto the linker. This flag causes thelinker to load every object file inthe library that defines anObjective-C class or category. Whilethis option will typically result in alarger executable (due to additionalobject code loaded into theapplication), it will allow thesuccessful creation of effectiveObjective-C static libraries thatcontain categories on existingclasses.
iPhone Development FAQ 中也有推荐:
How do I link all the Objective-Cclasses in a static library? Set theOther Linker Flags build setting to-ObjC.
和标志说明:
-all_load Loads all members of static archive libraries.
-ObjC Loads all members of static archive libraries that implement anObjective-C class or category.
-force_load (path_to_archive) Loads all members of the specified staticarchive library. Note: -all_loadforces all members of all archives tobe loaded. This option allows you totarget a specific archive.
*我们可以使用 force_load 来减少应用程序二进制文件大小并避免 all_load 在某些情况下可能导致的冲突。
是的,它适用于添加到项目中的 *.a 文件。然而,我在将 lib 项目添加为直接依赖项时遇到了麻烦。但后来我发现这是我的错 - 可能没有正确添加直接依赖项目。当我删除它并通过步骤再次添加时:
之后一切正常。在我的情况下,“-ObjC”标志就足够了。
我也对 http://iphonedevelopmentexperiences.blogspot.com/2010/03/categories-in-static-library.html 的想法很感兴趣博客。作者说他可以在不设置 -all_load 或 -Objcflags的情况下使用 lib 中的类别。他只是将空的虚拟类接口(interface)/实现添加到类别 h/m 文件中,以强制链接器使用此文件。是的,这个技巧可以完成这项工作。
但是作者也说他甚至没有实例化虚拟对象。嗯……正如我发现的那样,我们应该从类别文件中显式调用一些“真实”代码。所以至少应该调用类函数。我们甚至不需要虚拟类。单个 c 函数也是如此。
所以如果我们把lib文件写成:
// mylib.h
void useMyLib();
@interface NSObject (Logger)
-(void)logSelf;
@end
// mylib.m
void useMyLib(){
NSLog(@"do nothing, just for make mylib linked");
}
@implementation NSObject (Logger)
-(void)logSelf{
NSLog(@"self is:%@", [self description]);
}
@end
如果我们调用 useMyLib(); App 项目中的任何地方那么在任何类中我们都可以使用logSelf类方法;
[self logSelf];
还有更多关于主题的博客:
http://t-machine.org/index.php/2009/10/13/how-to-make-an-iphone-static-library-part-1/
http://blog.costan.us/2009/12/fat-iphone-static-libraries-device-and.html
关于iphone - 静态库中的 Objective-C 类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2567498/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!