use of @import when modules are disabled

34,274

Solution 1

I got this warning in a zero-swift project whenever I tried to add the @import SafariServices; statement.

Solution: Enable the modules. Go to the Target > Build Settings and set the Enable Modules (C and Objective-C modules) to YES.

I've circled the Build Settings toggle to change.

OR

Note: I haven't verified this potential solution, but probably worthy of consideration if there are side effects caused by this solution.

Rather than enabling modules to entire project, we can enable modules for a specific file which is importing c++ file. Go to build phases -> Compile Sources -> Select the file -> Add compiler flag -fmodules

Solution 2

The possible cause is that you use Objective-C++. Then modules get disabled despite the proper build settings.

Solution 3

I've been mixing ObjC, ObjC++, C++, and Metal. Whenever I get the "use of @import when modules are disabled" I try replacing:

@import Name; 

with:

#import "Name/Name.h"

example, replace:

@import Metal;
@import MetalKit;
@import CoreVideo;

with:

#import "Metal/Metal.h"
#import "MetalKit/MetalKit.h"
#import "CoreVideo/CoreVideo.h"

It seems to work.

Solution 4

Check if you are using #import "ProductName-Swift.h" somewhere in .mm files or any other files other than objc files.

Because if you use this import in cpp files then modules gets disabled automatically.

Share:
34,274
Admin
Author by

Admin

Updated on October 27, 2020

Comments

  • Admin
    Admin over 3 years

    I have a problem

    @import Foundation;
    

    and I see:
    @import vs #import - iOS 7

    and I set "Enable Modules" to "YES"

    and my problem is not solved

  • Josh
    Josh about 7 years
    Thanks! worked for me. PS: you forgot to censor your target name :-)
  • FranticRock
    FranticRock over 6 years
    I, at one point had .mm files in the project. They are no longer there. I double checked - no .mm files in project. Also, i have done everything already described in this thread. Still getting the same error. Modules are enabled on all targets. Foundation is imported. I checked the file types on all source files, they are Objective-C, not Objective-C++. Any suggestions?
  • Peter Brockmann
    Peter Brockmann over 6 years
    Thanks @Josh , much appreciated.
  • KarenAnne
    KarenAnne over 6 years
    I noticed the issue is caused by importing the framework (with @import Foundation) into a .mm file
  • E_Ri
    E_Ri over 6 years
    To my amazement, this didn't resolve the matter for me. Then after running out of ideas as I was sure THIS IS right, I restarted Xcode (9.2) and then it worked totally fine. Sigh.
  • Cerniuk
    Cerniuk about 6 years
    Xcode 9.3, struggled with same, did same, and happened to read your note Mr. Snell, quit Xcode and opened, and THAT completed the solution. (bad Xcode dog BAD :-)
  • Lysdexia
    Lysdexia over 5 years
    I am using Obj-C++, and facing this same problem. What should I do?
  • Quang Vĩnh Hà
    Quang Vĩnh Hà about 5 years
    you should create an Obj-C wrapper that use @import (if you actually need to)
  • Dmitry Kolesnikovich
    Dmitry Kolesnikovich about 4 years
    hundred times this! i removed @import Foundation from MyFramework.h and it works now!
  • mark.ed
    mark.ed about 4 years
    Thanks Peter - I've been trying to update parse and parse/ui on an old app to move it to sashido and i was banging my head agains the wall that none of the PF stuff was working!
  • Bruno Bieri
    Bruno Bieri about 4 years
    Thanks! Works for me too by replacing @import GoogleMobileAds; with #import "GoogleMobileAds/GADBannerView.h"