我们不再需要将框架链接到 XCode 项目了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24902787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Don't we need to link framework to XCode project anymore?
提问by Tar_Tw45
Base on this question
基于这个问题
Why don't iOS framework dependencies need to be explicitly linked to a static library
I read the selected answer and still don't understand so I made an example project
我阅读了选定的答案,但仍然不明白,所以我做了一个示例项目
In the test project, I remove all framework from Link Binary With Librariesand File navigationfor both main project and the static library (including Foundation.framework and UIKit.framework too), basically, both project link to 0 frameworks.
在测试项目中,我从主项目和静态库(包括 Foundation.framework 和 UIKit.framework )的链接二进制文件和文件导航中删除了所有框架,基本上,两个项目都链接到 0 个框架。
Questions are
问题是
- In static library, it's including MapKit/MapKit.h without referencing the Mapkit.framework to the project, why is its still working?
- In main project, I remove UIKit.framework and Foundation.framework from the project, why is it still working?
- Since it's working for now, will there be any issue later?
- 在静态库中,它包含 MapKit/MapKit.h 而没有将 Mapkit.framework 引用到项目,为什么它仍然有效?
- 在主项目中,我从项目中删除了 UIKit.framework 和 Foundation.framework,为什么它仍然有效?
- 既然现在可以用,以后会不会有问题?
Thank you for your comment.
感谢您的评论。
P.S. By working, I mean I can run on the simulator and I can archive the main project without any error.
PS 通过工作,我的意思是我可以在模拟器上运行,并且可以毫无错误地存档主项目。
Edit 25/07/2014
编辑 25/07/2014
I tried with the real app that I'm working on, it's the same.
我尝试了我正在开发的真实应用程序,它是一样的。
- I highlight Foundation, UIKit, CoreData and 10 another frameworks in File Navigation, well, all of them.
- Uncheck the target in Utilities Panel --> Target Membership
- Build : Pass, Run : Pass
- 我在文件导航中重点介绍了 Foundation、UIKit、CoreData 和 10 个其他框架,好吧,所有这些。
- 在实用程序面板中取消选中目标 --> 目标成员资格
- 构建:通过,运行:通过
Every functionality of my app is still working as expected. I don't get this.
我的应用程序的每个功能仍然按预期工作。我不明白这个。
回答by Tommy
Check your project build settings. Underneath LLVM 5.1 — Language — Modulesyou should see the option 'Link Frameworks Automatically'. In your case it sounds like it's set to 'YES', the default.
检查您的项目构建设置。在LLVM 5.1 — 语言 — 模块下,您应该会看到“自动链接框架”选项。在您的情况下,它听起来像是默认设置为“是”。
In that case, instead of producing an error when you reference a class that the compiler doesn't know, it'll figure out which Framework contains that class and link it. In your code it'll be MKMapView
or one of the other MapKit classes that triggers the linkage.
在这种情况下,当您引用编译器不知道的类时,它不会产生错误,而是会找出哪个框架包含该类并链接它。在您的代码中,它将MKMapView
是触发链接的其他 MapKit 类之一。
EDIT: from the relevant 'What's New?' document:
编辑:来自相关的'What's New?' 文件:
Auto Linking is enabled for frameworks imported by code modules. When a source file includes a header from a framework that supports modules, the compiler generates extra information in the object file to automatically link in that framework. The result is that, in most cases, you will not need to specify a separate list of the frameworks to link with your target when you use a framework API that supports modules.
为代码模块导入的框架启用自动链接。当源文件包含来自支持模块的框架的头文件时,编译器会在目标文件中生成额外信息以自动链接到该框架中。结果是,在大多数情况下,当您使用支持模块的框架 API 时,您无需指定单独的框架列表以与目标链接。
Another way of looking at it is that the compiler is smart enough to mutate #import
to @import
when the framework has been built appropriately. All system frameworks have been.
另一种看待它的方式是编译器足够聪明,#import
可以@import
在适当地构建框架时进行变异。所有的系统框架都是。
回答by Abhimanyu Yadav
To elaborate @Tommy's answer, a framework that supports modulessatisfies the following 2 conditions:
为了详细说明@Tommy 的回答,支持模块的框架满足以下 2 个条件:
Under Build Settings
> Packaging
在Build Settings
>Packaging
Define Modules
is set toYES
Module Map File
exists.
Define Modules
被设定为YES
Module Map File
存在。
So, if you're certain that the framework you're using in your code modularizes like that, you can choose to not explicitly add it in the link phase as it will be automatically added as long as in the project file, under Apple Clang - Language - Modules
, The option Link Frameworks Automatically
is set to YES
.
因此,如果您确定您在代码中使用的框架是这样模块化的,您可以选择不在链接阶段显式添加它,因为只要在项目文件中,它就会自动添加,在Apple Clang - Language - Modules
, 下选项Link Frameworks Automatically
设置为YES
。