@import vs #import - iOS 7

本文翻译自:@import vs #import - iOS 7

I am playing around with some of the new iOS 7 features and working with some of the Image Effects as discussed in the WWDC video "Implementing Engaging UI on iOS". 我正在玩一些新的iOS 7功能,并使用WWDC视频“在iOS上实现引导UI”中讨论的一些图像效果。 For producing a blur effect within the source code for the session, UIImage was extended via a category which imports UIKit like so: 为了在会话的源代码中产生模糊效果, UIImage通过导入UIKit的类别扩展,如下所示:

@import UIKit;

I think I saw something about this in another session video but I'm having trouble finding it. 我想我在另一个会话视频中看到了一些相关内容,但我找不到它。 I'm looking for any background information on when to use this. 我正在寻找有关何时使用此信息的任何背景信息。 Can it only be used with Apple frameworks? 它只能用于Apple框架吗? Are the benefits of using this compiler directive enough that I should go back and update old code? 使用此编译器指令的好处是否应该返回并更新旧代码?


#1楼

参考:https://stackoom.com/question/1HV76/import-vs-import-iOS


#2楼

It's a new feature called Modules or "semantic import". 这是一个名为模块或“语义导入”的新功能。 There's more info in the WWDC 2013 videos for Session 205 and 404 . 会话205404WWDC 2013视频中有更多信息。 It's kind of a better implementation of the pre-compiled headers. 这是预编译头文件的一种更好的实现。 You can use modules with any of the system frameworks in iOS 7 and Mavericks. 您可以将模块与iOS 7和Mavericks中的任何系统框架一起使用。 Modules are a packaging together of the framework executable and it's headers and are touted as being safer and more efficient than #import . 模块是框架可执行文件和它的标题的包装,被吹捧为比#import更安全,更高效。

One of the big advantages of using @import is that you don't need to add the framework in the project settings, it's done automatically . 使用@import一大优势是您不需要在项目设置中添加框架,它是自动完成的 That means that you can skip the step where you click the plus button and search for the framework (golden toolbox), then move it to the "Frameworks" group. 这意味着您可以跳过单击加号按钮并搜索框架(黄金工具箱)的步骤,然后将其移动到“框架”组。 It will save many developers from the cryptic "Linker error" messages. 它将使许多开发人员免于神秘的“链接器错误”消息。

You don't actually need to use the @import keyword. 您实际上不需要使用@import关键字。 If you opt-in to using modules, all #import and #include directives are mapped to use @import automatically. 如果您选择使用模块,则所有#import#include指令都会映射为自动使用@import That means that you don't have to change your source code (or the source code of libraries that you download from elsewhere). 这意味着您不必更改源代码(或从其他地方下载的库的源代码)。 Supposedly using modules improves the build performance too, especially if you haven't been using PCHs well or if your project has many small source files. 据推测,使用模块也可以提高构建性能,特别是如果你没有很好地使用PCH或者你的项目有很多小的源文件。

Modules are pre-built for most Apple frameworks (UIKit, MapKit, GameKit, etc). 模块是为大多数Apple框架(UIKit,MapKit,GameKit等)预先构建的。 You can use them with frameworks you create yourself: they are created automatically if you create a Swift framework in Xcode, and you can manually create a ".modulemap" file yourself for any Apple or 3rd-party library . 您可以将它们与您自己创建的框架一起使用:如果您在Xcode中创建Swift框架,它们会自动创建,您可以自己为任何Apple或第三方库手动创建“.modulemap”文件。

You can use code-completion to see the list of available frameworks: 您可以使用代码完成来查看可用框架的列表:

在此输入图像描述

Modules are enabled by default in new projects in Xcode 5 . Xcode 5中的新项目默认启用模块 To enable them in an older project, go into your project build settings, search for "Modules" and set "Enable Modules" to "YES". 要在较旧的项目中启用它们,请进入项目构建设置,搜索“模块”并将“启用模块”设置为“是”。 The "Link Frameworks" should be "YES" too: “Link Frameworks”也应该是“YES”:

You have to be using Xcode 5 and the iOS 7 or Mavericks SDK, but you can still release for older OSs (say iOS 4.3 or whatever). 您必须使用Xcode 5和iOS 7或Mavericks SDK,但您仍然可以发布较旧的操作系统(比如iOS 4.3或其他)。 Modules don't change how your code is built or any of the source code. 模块不会改变代码的构建方式或任何源代码。


From the WWDC slides: 来自WWDC幻灯片:

  • Imports complete semantic description of a framework 导入框架的完整语义描述
  • Doesn't need to parse the headers 不需要解析头文件
  • Better way to import a framework's interface 导入框架界面的更好方法
  • Loads binary representation 加载二进制表示
  • More flexible than precompiled headers 比预编译头更灵活
  • Immune to effects of local macro definitions (eg #define readonly 0x01 ) 免疫局部宏定义的影响(例如#define readonly 0x01
  • Enabled for new projects by default 默认情况下启用新项目

To explicitly use modules: 要明确使用模块:

Replace #import <Cocoa/Cocoa.h> with @import Cocoa; @import Cocoa;替换#import <Cocoa/Cocoa.h> @import Cocoa;

You can also import just one header with this notation: 您还可以使用此表示法导入一个标头:

@import iAd.ADBannerView;

The submodules autocomplete for you in Xcode. 子模块在Xcode中自动完成。


#3楼

It currently only works for the built in system frameworks. 它目前仅适用于内置系统框架。 If you use #import like apple still do importing the UIKit framework in the app delegate it is replaced (if modules is on and its recognised as a system framework) and the compiler will remap it to be a module import and not an import of the header files anyway. 如果您使用#import类的苹果,仍然会在app委托中导入UIKit框架,它会被替换(如果模块已启用且被识别为系统框架),编译器会将其重新映射为模块导入而不是导入无论如何头文件。 So leaving the #import will be just the same as its converted to a module import where possible anyway 因此,离开#import将尽可能与转换为模块导入相同


#4楼

There is a few benefits of using modules. 使用模块有一些好处。 You can use it only with Apple's framework unless module map is created. 除非创建模块映射,否则只能将其用于Apple的框架。 @import is a bit similar to pre-compiling headers files when added to .pch file which is a way to tune app the compilation process. @import有点类似于在添加到.pch文件时预编译头文件,这是一种调整应用程序编译过程的方法。 Additionally you do not have to add libraries in the old way, using @import is much faster and efficient in fact. 此外,您不必以旧方式添加库,使用@import实际上更快更有效。 If you still look for a nice reference I will highly recommend you reading this article . 如果你仍然寻找一个很好的参考,我强烈建议你阅读这篇文章


#5楼

Nice answer you can find in book Learning Cocoa with Objective-C (ISBN: 978-1-491-90139-7) 您可以在书中学习可可与Objective-C(ISBN:978-1-491-90139-7)找到很好的答案

Modules are a new means of including and linking files and libraries into your projects. 模块是一种将文件和库包含和链接到项目中的新方法。 To understand how modules work and what benefits they have, it is important to look back into the history of Objective-C and the #import statement Whenever you want to include a file for use, you will generally have some code that looks like this: 要了解模块如何工作以及它们有什么好处,重要的是回顾一下Objective-C和#import语句的历史。每当您想要包含一个文件以供使用时,通常会有一些代码如下所示:

#import "someFile.h"

Or in the case of frameworks: 或者在框架的情况下:

#import <SomeLibrary/SomeFile.h>

Because Objective-C is a superset of the C programming language, the #import state‐ ment is a minor refinement upon C's #include statement. 因为Objective-C是C编程语言的超集,所以#import语句是对C的#include语句的一个小改进。 The #include statement is very simple; #include语句非常简单; it copies everything it finds in the included file into your code during compilation. 它会在编译期间将它在包含文件中找到的所有内容复制到您的代码中。 This can sometimes cause significant problems. 这有时会导致严重的问题。 For example, imagine you have two header files: SomeFileA.h and SomeFileB.h ; 例如,假设您有两个头文件: SomeFileA.hSomeFileB.h ; SomeFileA.h includes SomeFileB.h , and SomeFileB.h includes SomeFileA.h . SomeFileA.h包含SomeFileB.hSomeFileB.h包含SomeFileA.h This creates a loop, and can confuse the coimpiler. 这会创建一个循环,并且可能会混淆coimpiler。 To deal with this, C programmers have to write guards against this type of event from occurring. 为了解决这个问题,C程序员必须针对此类事件编写防范措施。

When using #import , you don't need to worry about this issue or write header guards to avoid it. 使用#import ,您无需担心此问题或编写标题保护以避免此问题。 However, #import is still just a glorified copy-and-paste action, causing slow compilation time among a host of other smaller but still very dangerous issues (such as an included file overriding something you have declared elsewhere in your own code.) 但是, #import仍然只是一个美化的复制和粘贴操作,导致许多其他较小但仍然非常危险的问题(例如,包含的文件覆盖您在自己的代码中已在其他地方声明的内容)的编译时间较慢。

Modules are an attempt to get around this. 模块试图解决这个问题。 They are no longer a copy-and-paste into source code, but a serialised representation of the included files that can be imported into your source code only when and where they're needed. 它们不再是复制并粘贴到源代码中,而是包含的文件的序列化表示,只有在需要时才能将其导入到源代码中。 By using modules, code will generally compile faster, and be safer than using either #include or #import . 通过使用模块,代码通常编译得更快,并且比使用#include或#import更安全。

Returning to the previous example of importing a framework: 回到上一个导入框架的示例:

#import <SomeLibrary/SomeFile.h>

To import this library as a module, the code would be changed to: 要将此库作为模块导入,代码将更改为:

@import SomeLibrary;

This has the added bonus of Xcode linking the SomeLibrary framework into the project automatically. 这有一个额外的好处,Xcode自动将SomeLibrary框架链接到项目中。 Modules also allow you to only include the components you really need into your project. 模块还允许您仅将实际需要的组件包含在项目中。 For example, if you want to use the AwesomeObject component in the AwesomeLibrary framework, normally you would have to import everything just to use the one piece. 例如,如果要在AwesomeLibrary框架中使用AwesomeObject组件,通常必须导入所有内容才能使用单件。 However, using modules, you can just import the specific object you want to use: 但是,使用模块,您只需导入要使用的特定对象:

@import AwesomeLibrary.AwesomeObject;

For all new projects made in Xcode 5, modules are enabled by default. 对于在Xcode 5中创建的所有新项目,默认情况下启用模块。 If you want to use modules in older projects (and you really should) they will have to be enabled in the project's build settings. 如果您想在旧项目中使用模块(并且您确实应该使用),则必须在项目的构建设置中启用它们。 Once you do that, you can use both #import and @import statements in your code together without any concern. 一旦这样做,您可以在代码中同时使用#import@import语句,而无需担心。


#6楼

It seems that since XCode 7.xa lot of warnings are coming out when enabling clang module with CLANG_ENABLE_MODULES 看来,当使用CLANG_ENABLE_MODULES启用clang模块时,XCode 7.xa会出现很多警告

Take a look at Lots of warnings when building with Xcode 7 with 3rd party libraries 使用Xcode 7和第三方库构建时,请查看大量警告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值