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

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

问题描述

我正在尝试一些新的 iOS 7 功能并使用一些图像效果,如 WWDC 视频在 iOS 上实现引人入胜的 UI"中所述.为了在会话的源代码中产生模糊效果,UIImage 通过一个类别进行扩展,该类别像这样导入 UIKit:

@import UIKit;

我想我在另一个会话视频中看到了一些关于此的内容,但我找不到它.我正在寻找有关何时使用它的任何背景信息.它只能与Apple框架一起使用吗?使用这个编译器指令的好处是否足以让我回去更新旧代码?

解决方案

这是一个名为模块或语义导入"的新功能.在 WWDC 2013 视频中提供了更多信息,用于会话 205404.这是预编译头文件的更好实现.您可以将模块与 iOS 7 和 Mavericks 中的任何系统框架一起使用. 模块是框架可执行文件及其标头的包装,并被吹捧为比 #import 更安全、更高效.

使用@import 的一大优点是您不需要在项目设置中添加框架,它会自动完成.这意味着您可以跳过单击加号按钮并搜索框架(金色工具箱)的步骤,然后将其移至框架"团体.它将使许多开发人员免于神秘的链接器错误"消息.

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

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

您可以使用代码完成来查看可用框架的列表:

Xcode 5 中的新项目默认启用模块.要在旧项目中启用它们,请进入您的项目构建设置,搜索模块".并设置启用模块"到是".链接框架"应该是是"还有:

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

<小时>

来自 WWDC 幻灯片:

<块引用>
  • 导入框架的完整语义描述
  • 不需要解析标题
  • 导入框架接口的更好方法
  • 加载二进制表示
  • 比预编译头更灵活
  • 不受局部宏定义的影响(例如#define readonly 0x01)
  • 默认为新项目启用

<小时>

要明确使用模块:

#import 替换为 @import Cocoa;

您也可以使用此符号仅导入一个标题:

@import iAd.ADBannerView;

子模块在 Xcode 中自动完成.

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;

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?

解决方案

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 its headers and are touted as being safer and more efficient than #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.

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.

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.

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

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:

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.


From the WWDC slides:

  • 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


To explicitly use modules:

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

You can also import just one header with this notation:

@import iAd.ADBannerView;

The submodules autocomplete for you in Xcode.

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

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