@import vs #import - iOS 7
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.
Objective C @import for modules does it replace #import?
for you including of your project files do you continue to use #import or does @import completely replace it?
@import
, so far, is for Apple frameworks only, so at the time of writing you still have to use #import
for anything else.
The good news is that, if you opt-in, any #import
will be implicitly replaced for you by the compiler, so you don't need to convert your previous code to benefit from modules.
Also, can I define my own modules easily?
Yes and no.
Yes, it's easy, but...
...no you cannot, since this feature is currently not supported for non-Apple frameworks.
To define your own module - if you could - you would need to do:
export MyAwesomeModule:
public:
// methods and whatever you want to export
What is the difference between #import and #include in Objective-C?
The #import directive was added to Objective-C as an improved version of #include. Whether or not it's improved, however, is still a matter of debate. #import ensures that a file is only ever included once so that you never have a problem with recursive includes. However, most decent header files protect themselves against this anyway, so it's not really that much of a benefit.
Basically, it's up to you to decide which you want to use. I tend to #import headers for Objective-C things (like class definitions and such) and #include standard C stuff that I need. For example, one of my source files might look like this:
#import <Foundation/Foundation.h>
#include <asl.h>
#include <mach/mach.h>
Related Topics
How to Make a Uitextfield Move Up When the Keyboard Is Present - on Starting to Edit
How to Programmatically Send Sms on the Iphone
Setting the Datepicker in iOS to Pick Only the Month and Year
How to Find Current Visible Viewcontroller in Ios
Symbolicating Iphone App Crash Reports
Attempt to Present Uiviewcontroller on Uiviewcontroller Whose View Is Not in the Window Hierarchy
Iphone/Ipad: How to Make Uitextfield Readonly (But Not Disabled)
Ios Swift - Objective C Code Migration to Swift
How to Store Custom Objects in Nsuserdefaults
Get Current Scroll Position of Scrollview in React Native
Ios: How to Get a Proper Month Name from a Number
React Native App Crashes on Launch Screen on Device
How to Embed a Custom Font in an Iphone Application
How to Center Horizontally Uicollectionview Cells
How to Remove Constraints Programmatically That Is Added from Storyboard
How to Pass Prepareforsegue: an Object