use of @import when modules are disabled
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.
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.
Admin
Updated on October 27, 2020Comments
-
Admin over 3 years
I have a problem
@import Foundation;
and I see:
@import vs #import - iOS 7and I set "Enable Modules" to "YES"
and my problem is not solved
-
Josh about 7 yearsThanks! worked for me. PS: you forgot to censor your target name :-)
-
FranticRock over 6 yearsI, 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 over 6 yearsThanks @Josh , much appreciated.
-
KarenAnne over 6 yearsI noticed the issue is caused by importing the framework (with @import Foundation) into a .mm file
-
E_Ri over 6 yearsTo 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 about 6 yearsXcode 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 over 5 yearsI am using Obj-C++, and facing this same problem. What should I do?
-
Quang Vĩnh Hà about 5 yearsyou should create an Obj-C wrapper that use @import (if you actually need to)
-
Dmitry Kolesnikovich about 4 yearshundred times this! i removed
@import Foundation
from MyFramework.h and it works now! -
mark.ed about 4 yearsThanks 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 about 4 yearsThanks! Works for me too by replacing
@import GoogleMobileAds;
with#import "GoogleMobileAds/GADBannerView.h"