@import--iOS7新关键字

一、好处

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的最大好处之一是当需要使用某个苹果自己的框架时,再也不需要在项目的setting中做 "点击'+'号按钮搜索依赖框架添加" 这样的操作了,@import内部会自动实现这个过程。这样我们就可以使用代码完成以前需要在图形界面完成的动作。苹果又为开发者做出了改进。

以前写程序导入系统头文件的时候都这么写#import <Cocoa/Cocoa.h>, 现在你多了一种选择, 你可以这么写:@import Cocoa; 并且免去了添加框架的操作。

二、局限性

Modules only work with Apple frameworks (UIKit, MapKit, GameKit, etc). You can't use them with frameworks you create yourself. Currently you can't use them with 3rd-party frameworks (AFNetworking, RestKit, MagicalRecord). I don't think it works with non-Apple frameworks even if they're included in Xcode (dylibs like libsqlite3.dylib).

但是目前使用@import仅适用于苹果自己的框架,当在项目中想使用@import导入自己搭建的框架或者第三方框架时,是无效的。

三、实际开发中

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.

实际开发中上可能并不需要使用@import关键字。如果你选择使用"modules"(Xcode5中默认开启),所有的#import和#include 指令都会自动映射使用@import。这意味着你并不需要改变你之前的源码。下面意译, 尤其对于没有善用.pch头文件的开发者或者你的项目中有许多零碎小的源文件时, 使用@import导入指定框架下需要的头文件时,Xcode的编译表现会有提高。

读完这篇回答我个人的理解就是, 苹果再次针对开发者经常会遇到的问题做出了改进,提供了图形化操作之外的代码方式添加框架的选择, 更加的灵活人性。当然可能@import在实际开发中并不会对工作有多少帮助,善于使用pch文件并且尽量避免零碎的小文件, 项目的整体编译性能才会有提升。

这篇回答本身英文并不难理解,本来没想翻译,但是想到可能会有同样疑惑的开发者,于是便将主要内容翻译出来,自己印象也深刻一点,有翻译不到位或者理解错误的地方还请纠正。

原文出处:

http://stackoverflow.com/questions/18947516/import-vs-import-ios-7

原文地址:https://www.cnblogs.com/orzmj123/p/3502041.html