xcodeframeworksios-frameworksxcode14xcode-build-settings

Why does Apple discourage the use of '@import' in framework headers?


I have a framework target which contains a mixture of Objective-C and Swift source files.

I just upgraded my project's build settings to the settings recommended by Xcode 14.3.1.

One of the changes made by Xcode was to turn on the ENABLE_MODULE_VERIFIER build setting for my framework target.

Following this upgrade, I get the following build error in all of the public header files of my framework which contain a semantic import (i.e. an import of the form @import MODULE_NAME;):

use of '@import' in framework header is discouraged, including this header requires -fmodules

I see this advice communicated in the Xcode > Build system > Identifying and addressing framework module issues article also:

Avoid the use of semantic import syntax in your public and private headers.

Neither the Xcode build error nor the article explain why we should avoid semantic imports in our frameworks' public and private header files.

Why should we avoid semantic imports in our frameworks' public and private header files?


Solution

  • The stated reason from Apple in the release notes for Xcode 11.4 (the release that made it a default warning) is "using @import in framework headers is discouraged, because doing so requires all importers to use modules."

    In other words, using it forces a dependency relationship as opposed to a normal import statement where the preprocessor simply includes the necessary aspects of another file.