Objective-C
iOS 7
@import
#import
iOS Development

import vs import - iOS 7

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the context of iOS development, particularly with Objective-C, understanding the difference between @import and #import can be crucial for efficient coding. Introduced with iOS 7 and Xcode 5, @import provides some advantages over #import, especially in terms of modularity, compilation time, and namespace pollution. Here's an in-depth look at both:

Understanding #import

What is #import?

#import is a preprocessor directive in C and Objective-C which is used to include files. It ensures that a file is included only once per translation unit, preventing duplicate declaration errors. This is an improvement over the traditional #include used in C, which could result in repeated inclusions without additional safeguards.

Example Usage

objective-c
#import <UIKit/UIKit.h>
#import "MyCustomView.h"

Technical Details

  • Duplication Prevention: #import automatically prevents the same file from being included multiple times, which is a manual task when using #include.
  • Preprocessor Directive: As part of the preprocessor, #import directives are handled before the actual compilation process.

Enter @import

What is @import?

Introduced in iOS 7, @import is designed to work with modules, which are logical units of code distribution. Modules provide a way to package headers and implementations together, leading to an improved import system over traditional preprocessor directives.

Example Usage

objective-c
@import UIKit;
@import Foundation;

Technical Details

  • Modular System: @import takes advantage of the modular architecture, enabling faster compilation and better encapsulation.
  • Namespace Management: Modules help avoid name clashes by separating the global namespace across packages.
  • Dependable Compilation and Consistency: Offers a safer way to include frameworks, ensuring availability and version consistency.
  • Compilation Speed: By utilizing @import, build times can be reduced because the system uses precompiled information instead of parsing and processing header files repeatedly.

Key Differences

Here's a table summarizing the key differences between @import and #import:

Feature#import@import
TypePreprocessor DirectiveModule Import
Prevention of DuplicatesAutomaticAutomatic
Compilation SpeedSlower, due to repeated parsingFaster, due to precompiled data
Namespace ManagementLimited (risk of clashes)Improved (dedicated namespaces)
Usage with FrameworksRequires manual path specificationAutomatic framework linkage
AvailabilityC, Objective-C, C++Objective-C (iOS 7+, OS X 10.9+)

Enhanced Details

Usage Scenarios

When to Use @import:

  • Modern Projects: Given module advantages, modern iOS projects benefit from @import for cleaner and faster imports, especially when working with large frameworks.
  • Decoupling and Modularity: If you're aiming for a modular design that aligns better with Swift and modern Objective-C improvements, using @import should be preferred.

When to Use #import:

  • Legacy Codebases: In situations where the project code base is extensive and started before iOS 7, maintaining consistency with existing #import directives can be advisable.
  • Compatibility Considerations: If you need to maintain compatibility with very old systems that might not support modules.

Transitioning from #import to @import

Projects in transition from traditional Objective-C code can gradually adopt @import by:

  1. Identifying frequently used frameworks.
  2. Replacing #import statements with @import progressively.
  3. Verifying that module support is enabled in the build settings of the project.

Conclusion

While both @import and #import serve the purpose of importing code files and libraries, @import has significant advantages when working with modules. For projects targeting iOS 7 and newer, incorporating @import can streamline development, offer namespace protection, and improve build performance. However, understanding and leveraging both techniques effectively can contribute substantially to maintaining and evolving a sophisticated code base.


Course illustration
Course illustration

All Rights Reserved.