59

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

Community
  • 1
  • 1
Ali Rezaei
  • 633
  • 1
  • 5
  • 7

4 Answers4

94

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

Peter Brockmann
  • 3,404
  • 2
  • 24
  • 26
  • 1
    Thanks! worked for me. PS: you forgot to censor your target name :-) – Josh Apr 21 '17 at 10:10
  • Thanks @Josh , much appreciated. – Peter Brockmann Aug 08 '17 at 12:47
  • 5
    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. – James T Snell Dec 20 '17 at 08:47
  • 1
    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 :-) – Cerniuk Apr 07 '18 at 13:30
  • 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! – mark.ed Apr 21 '20 at 17:23
37

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

Aleks N.
  • 5,618
  • 2
  • 39
  • 40
  • 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? – FranticRock Aug 04 '17 at 01:57
  • 2
    I noticed the issue is caused by importing the framework (with @import Foundation) into a .mm file – KarenAnne Oct 30 '17 at 08:51
  • 2
    I am using Obj-C++, and facing this same problem. What should I do? – Lysdexia Aug 22 '18 at 07:25
  • you should create an Obj-C wrapper that use @import (if you actually need to) – Quang Vĩnh Hà Apr 10 '19 at 10:46
  • hundred times this! i removed `@import Foundation` from MyFramework.h and it works now! – Dmitry Kolesnikovich Apr 06 '20 at 23:46
20

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.

Warren Stringer
  • 1,349
  • 1
  • 12
  • 11
  • 2
    Thanks! Works for me too by replacing `@import GoogleMobileAds;` with `#import "GoogleMobileAds/GADBannerView.h"` – Bruno Bieri Apr 27 '20 at 14:55
11

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.

Uday Sravan K
  • 799
  • 8
  • 15