@import vs #import - iOS 7 [英] @import vs #import - iOS 7

查看:210
本文介绍了@import vs #import - iOS 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一些新的iOS 7功能,并使用WWDC视频在iOS上实现引导UI中讨论的一些图像效果。为了在会话的源代码中产生模糊效果, UIImage 通过导入UIKit的类别扩展,如下所示:

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". For producing a blur effect within the source code for the session, UIImage was extended via a category which imports UIKit like so:

@import UIKit;

我想我在另一个会话视频中看到了这个,但我找不到它。我正在寻找有关何时使用此功能的任何背景信息。它只能用于Apple框架吗?使用这个编译器指令的好处是我应该返回并更新旧代码吗?

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? Are the benefits of using this compiler directive enough that I should go back and update old code?

推荐答案

这是一个名为<的新功能strong>模块或语义导入。会话 WWDC 2013 视频中有更多信息.apple.com / videos / play / wwdc2013 / 205 /rel =noreferrer> 205 和 404 。这是预编译头文件的更好实现。 您可以将模块与iOS 7和Mavericks中的任何系统框架一起使用。模块是框架可执行文件及其标题的打包,并被吹捧为比 #import 。

It's a new feature called Modules or "semantic import". There's more info in the WWDC 2013 videos for Session 205 and 404. 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. 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. 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.

您实际上不需要使用 @import keyword。如果您选择使用模块,则所有 #import #include 指令被映射为自动使用 @import 。这意味着您不必更改源代码(或从其他地方下载的库的源代码)。据推测,使用模块也可以提高构建性能,特别是如果你没有很好地使用PCH或者你的项目有很多小的源文件。

You don't actually need to use the @import keyword. If you opt-in to using modules, all #import and #include directives are mapped to use @import automatically. 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.

模块是预先构建的大多数Apple框架(UIKit,MapKit,GameKit等)。您可以将它们与您自己创建的框架一起使用:如果您在Xcode中创建Swift框架,它们会自动创建,您可以自己手动为任何Apple或第三方库

Modules are pre-built for most Apple frameworks (UIKit, MapKit, GameKit, etc). 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.

您可以使用代码-completion查看可用框架列表:

You can use code-completion to see the list of available frameworks:

Xcode 5中的新项目默认启用模块。要在较旧的项目中启用它们,请进入项目构建设置,搜索模块并将启用模块设置为是。 Link Frameworks也应为YES:

Modules are enabled by default in new projects in 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:

你必须使用Xcode 5和iOS 7或Mavericks SDK,但你仍然可以发布旧操作系统(比如iOS 4.3或其他)。 模块不会更改代码的构建方式或任何源代码。

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). Modules don't change how your code is built or any of the source code.

来自WWDC幻灯片:



  • 导入框架的完整语义描述

  • 不需要解析标题

  • 导入框架界面的更好方法

  • 加载二进制表示

  • 比预编译标题更灵活

  • 免疫本地宏定义的效果(例如 #define readonly 0x01

  • 默认情况下为新项目启用

  • 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 (e.g. #define readonly 0x01)
  • Enabled for new projects by default






明确使用模块:

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

您还可以使用此表示法导入一个标题:

You can also import just one header with this notation:

@import iAd.ADBannerView;

Xcode中的子模块自动完成。

The submodules autocomplete for you in Xcode.

这篇关于@import vs #import - iOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆