I am getting error when trying to import .mm file to another one.
Its build like that :
first class FailedMenuLayer is .mm and have in its .h :
#import "gameScene.h"
second class gameScene is .mm, and have in its .h :
#import "FailedMenuLayer.h"
FailedMenuLayer *menuGO; //here i get the error: unknown type FailedMenuLayer .
why is that ?
It looks like an import cycle.
One way to fix it is to move the "gameScene.h" import to the .mm file. It's actually a good practice to keep the imports in the .h file limited only to what you actually need in the header and keep everything else in the .mm file.
If you need the import in the header try using #class instead of #import;
#class gameScene;
Don't forget to import gameScene.h in the .mm file also.
you are not importing ".mm" file, you are importing it's header.
Check your build phases> compile sources for your .mm file to be listed there. That might be your issue
Related
Is it possible to import the modulename-Swift.h file to another .h file, so that the test target also would compile?
Currently, I was importing the modulename-Swift.h in the one of the headers of the app's target, however, the test target was not able to compile.
When I moved the import statement to the .m file instead, I was able to compile both, the app and the tests.
However, I have to resort to a forward protocol declaration in order to resolve this issue - the modulename-Swift.h file contains a protocol.
So, the question is whether I can import that file in .h file at all?
No, you can't import modulename-Swift.h in a .h file. You'll need to create forward declarations (adding #protocol Something; to your .h) and import the Swift module in the .m file.
Another way to work around this is to declare the protocol conformance in a category in the .m file. More details can be found in this StackOverflow answer:
https://stackoverflow.com/a/27626493/3208043
I'm trying to import a Swift class from my framework MyFramework in my unit test code:
In my Objective-C test file, I do:
#import MyFramework;
This line causes a compilation error:
Include of non-modular header inside framework module 'MyFramework': 'src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h'
inside the "MyFramework-Swift.h" file. In that file, I can see a plain import from
#import "src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h"
I'm not sure what to do. My framework "defines" modular headers, but it sound like the problem is that bridging header file which is included with its entire path rather than just doing #import <MyFramework/MyFramework-Bridging-Header.h>.
Any help appreciated!
While calling swift class in objective c
In Bridging-Header add #import "myClass.h". This is objective c class
where you are using swift class.
In myClass.m add #import < yourProjectName_swift.h >
In myClass.h or myClass.m add "#swiftcalss;"
first or second point may cause to your error.
When creating a new Objective-C class, XCode inserts this line into the .h file:
#import <Foundation/Foundation.h>
Instead, I want it to be the following automatic:
#import Foundation;
Where can I set it?
Xcode inserts the imports because this is how it is defined in the "new file templates". Instead of changing those built-in templates you should clone them and make your own, with adjusted import statement.
Look at this answer how to do that: https://stackoverflow.com/a/33743/396578
I try to mix swift and Obj-c in my project.
I made a couple of Swift classes (and protocolls).
If I put #import "ModuleName-Swift.h" to an .m file, it's working properly, but if i try to put it to a .h file, it says "ModuleName-Swift.h" file not found.
What could be the problem?
import <ModuleName-Swift.h>
try using angle brackets instead.
My project has been increasing in size and I'm a little confused about where should I #import header files.
There are 3 main locations where I can import headers:
The .pch file (prefix)
The .h file (header)
the .m file (implementation)
I don't care if the compiler takes more time to compile the files, all I care is that the end product is as fast as possible and uses the least amount of memory.
So, my questions are:
If most files need a specific header, is it ok to add it to the .pch file, or is it more efficient to add it to just the required files?
Should imports be done in the .h or .m file? I know I must add it to the .h file if i'm going to declare it there, but if I don't need to declare it in the .h file is there a problem of leaving the import there?
No, it is not ok to include it into the .pch file. This file is precompiled to every module in the project. Read about it here.
Read this question and answer.
Put your imports in your .m whenever you can. If you are using a class in your .h use #class to forward the declaration, then #import in your .m. The only time you should import in your .h are protocols that you implement or superclasses.